Comments (6)
Hi Jens,
first of all, thanks for the feedback and asking the question :-)
I've thought about this in the past, as well. Unfortunately, I haven't come up with the silver bullet for this. In the end, all the class file import knows, is the file URL of a class, which is in general not enough, to know if a class is test code.
However, I did include a best guess for Maven and Gradle (as well as IntelliJ Idea). It uses the fact, that Maven compiles test classes to target/test-classes
and Gradle compiles classes to build/classes/test
(at least the versions I've checked).
The ClassFileImporter allows to specify ImportOptions
, which are pretty much just filters on URLs. If you use the JUnit style to write tests, you can thus write
@AnalyzeClasses(..., importOption = ImportOption.DontIncludeTests.class)
public class MyTest{
// ...
}
This will simply exclude all URLs that contain '/test-classes/' or '/test/', which should leave exactly production code (except if you put your whole project in a folder called 'test', as I said, it's just a best guess). If this is not good enough, you can just implement ImportOption
, tailor it specifically to your setup, and refer to it from @AnalyzeClasses
.
So you can put your tests for test code and your tests for production code in two different test classes and use different import options.
Is this good enough for your usecase?
NOTE: I've just noticed, that I was a little sloppy on the implementation of DontIncludeTests.class and hardcoded the *nix file separator. This is fixed on master, but if you're developing on Windows, the provided ImportOption of version 0.4.0 will probably not work correctly. If so, you could just copy DontIncludeTests from https://github.com/TNG/ArchUnit/blob/master/archunit/src/main/java/com/tngtech/archunit/core/importer/ImportOption.java and use that for your tests.
from archunit.
Aaaaaaah, you're absolutely right, now I remember, I wasn't sloppy about the file separator, I though "thank God we're dealing with URIs here and don't have to worry about the file separator" *facepalm*.
Thanks for paying attention, shouldn't do those things at midnight... :-( Especially with platform dependent stuff, where I don't have any test environment.
Anyway, glad to hear it works for you now :-) If you want to use @AnalyzeClasses
for the caching, you can still just make your own ImportOption
, that just returns dontImportTests.includes(location) && dontImportJars.includes(location)
.
Nevertheless, this got me thinking that the behavior could be more consistent. Thus from 0.5.0 on, importOption
in @AnalyzeClasses
will be an array importOptions
(even though this will be a breaking change, hope the easy migration will make it acceptable).
I've also applied your suggestion, DontIncludeTests
is now looking specifically for /build/classes/test/
and /target/test-classes/
, which should make it less confusing, if some folder test
exists somewhere within the project location.
Since you say you got it working, I'll pull this change in and close this issue :-)
from archunit.
Hi Jens, do you want to just disable all test rules for classes in src/test/java
or disable some of your rules?
from archunit.
Hi Johannes,
I think both make sense.
- To keep it simple it should be possible to completely take test code out of the equation.
- Advancing further one especially might want to introduce assertions about how unit and integration tests are written.
from archunit.
Hi Peter,
thanks for your thoughts on this subject. I looked here: ArchRule rule = noClasses().that().|
, but I understand, that there is no information about the origin of the class file at this point.
The ImportOption filters look promising. With @AnalyzeClasses
you can only specify one importOption
, but I'll try to call ClassFileImporter
with DontIncludeJars
and DontIncludeTests
. Thanks for mentioning the file separator issue.
from archunit.
Ok, I got it working. :-)
private final JavaClasses classes = new ClassFileImporter()
.importClasspath(new ImportOptions()
.with(new ImportOption.DontIncludeJars())
.with(new ImportOption.DontIncludeTests()));
Two small suggestions:
- the new check with
File.separator
is not supposed to work, asLocation.contains()
checks the URI, and this should always contain forward-slashes/
- you might want to be more strict about the checked URI path segments. How about
/build/classes/test/
and/target/test-classes/
from archunit.
Related Issues (20)
- Get modifiers of JavaParameter HOT 1
- Layer dependency rule ignores "catch" blocks
- Enum classes not recognized as enums and with with wrong access modifiers HOT 3
- Wrong line numbers and constructor call order
- JavaType#getAllInvolvedRawTypes() throws StackOverflowError with self-referential generic types HOT 2
- @AnalyzeClasses breaks JUnit 5 tag filtering HOT 3
- Is there a way to know if JavaCodeUnit is a switch/case statement? HOT 4
- Custom Error Messages for StoreUpdateFailedException
- Remove false positives from Transactional Proxy Rule HOT 2
- question about calling line numbers HOT 3
- Using MethodReferences is still breaking soundness HOT 5
- Usability: Java confuses the static and non-static versions of DescribedPredicate.and() when using the API the wrong way. HOT 3
- Unable to find the JavaAccess call HOT 8
- Feature suggestion: Freeze files and stored.rules validation / sanity check
- How To : validate return type is not `void` HOT 2
- How To : Is there way to filter classes when using `importJar()` HOT 1
- DEPRECATED_API_SHOULD_NOT_BE_USED does not consider constants nor final fields HOT 2
- ASM is not up-to-date
- Jakarta Inject package name incorrect in BE_ANNOTATED_WITH_AN_INJECTION_ANNOTATION rule HOT 1
- How can I find out which gradle module a class belongs to HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from archunit.