Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up JUnit 5 support discovery process #881

Merged
merged 2 commits into from
Jun 22, 2022

Conversation

codecholeric
Copy link
Collaborator

The JUnit platform API makes it necessary for ArchUnit to provide an implementation to discover test classes from the classpath (e.g. because the discovery request contains a package selector). Since ArchUnit itself is perfectly suited for this and does not demand any third party dependency we simply use the ClassFileImporter for this. However, so far we have used the current ArchConfiguration of the user, which by default e.g. resolves all further dependencies from the classpath. This is unnecessary overhead and can substantially slow down the discovery process if the test classes e.g. have some framework dependencies like the Spring framework.
We will now switch the ArchConfiguration for the discovery process so that it does not import any transitive dependencies of the discovered classes. This way the discovery process via ArchUnit should be quite fast, no matter what further dependencies the test classes have.

Resolves: #546

So far `ClassResolver` implementations had to have a constructor that is accessible to the `ClassResolverFactory`. This can be a little limiting if we just want to reconfigure the `ClassResolver` with a private class in a narrow scope. Thus, we now use Reflection to allow access also to non-accessible (e.g. private) classes.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
The JUnit platform API makes it necessary for ArchUnit to provide an implementation to discover test classes from the classpath (e.g. because the discovery request contains a package selector). Since ArchUnit itself is perfectly suited for this and does not demand any third party dependency we simply use the `ClassFileImporter` for this. However, so far we have used the current `ArchConfiguration` of the user, which by default e.g. resolves all further dependencies from the classpath. This is unnecessary overhead and can substantially slow down the discovery process if the test classes e.g. have some framework dependencies like the Spring framework.
We will now switch the `ArchConfiguration` for the discovery process. However, the global nature of `ArchConfiguration` does not make this completely trivial. E.g. if the user has another import in progress in another thread we do not want to affect this import by suddenly switching the user's configuration in the middle. To isolate this configuration change I have added a possibility to override the `ArchConfiguration` thread locally. I.e. the discovery process will only affect the current thread when changing the configuration and only during the scope of a fixed lambda. Afterwards the thread local configuration is cleaned up. This way we should never affect any other import of a user, since the scope of configuration change is tailored exactly to the discovery process and cleaned up afterwards.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
@codecholeric codecholeric force-pushed the speed-up-JUnit5-support-discovery-process branch from a85f832 to f818be3 Compare June 22, 2022 09:05
@codecholeric codecholeric merged commit 0489934 into main Jun 22, 2022
@codecholeric codecholeric deleted the speed-up-JUnit5-support-discovery-process branch June 22, 2022 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ArchUnit is run regardless of selected tests scope with JUnit5 in IntelliJ IDEA
1 participant