Comments (7)
Roaster looks neat, I might play around with that myself a little 😉 Might cover the source aspect that is missing from ArchUnit, for static code analysis.
For your use case, you should be fine, to do sth. like this:
@Test
public void only_use_Hamcrest() {
JavaClasses classes = new ClassFileImporter().importPackages("com.myapp");
ArchRule rule = noClasses()
.should().accessClassesThat()
.haveFullyQualifiedName(org.assertj.core.api.Assertions.class.getName())
.orShould().accessClassesThat()
.resideInAPackage("org.testng")
.because("we consistently want to use Hamcrest in our tests");
rule.check(classes);
}
(There is no specific TestNG support for ArchUnit up to now, like the ArchUnitRunner
for JUnit 4)
from archunit.
Hi Georgy, I think what you want is possible, but there might be several ways, so it depends a little on your use case. First of all, ArchUnit has no concept of imports, since it only analyzes bytecode.
However, I think you could achieve what you want either by naming:
@ArchTest
public static final ArchRule rule_via_package = noClasses().that().haveNameNotMatching(".*Test")
.should().accessClassesThat().resideInAPackage("org.junit..");
That works, if your external library has a distinguishing package. Or you could use the source of your Java classes, which is not integrated in the default fluent API, but can easily be added:
@ArchTest
public static final ArchRule rule_via_source =
noClasses().that(haveSourceMatching(".*/test/.*"))
.should().accessClassesThat(haveSourceMatching(".*junit.*\\.jar.*"));
private static DescribedPredicate<JavaClass> haveSourceMatching(final String regex) {
return new DescribedPredicate<JavaClass>("have source matching '%s'", regex) {
@Override
public boolean apply(JavaClass input) {
return input.getSource().isPresent()
&& input.getSource().get().getUri().toString().matches(regex);
}
};
}
Of course you can mix and match those approaches, like restricting the classes by name and the targets by source, etc.
Note that this only catches accesses though, so a dependency like an annotation is not captured. I'm working on improving this in the next version (you could add this yourself though, if you need it, I can also copy & paste some code here 😉)
But for test assertions it should work, since those are real accesses in bytecode, i.e. method calls, etc.
Does this help you?
from archunit.
Peter, I'll try your solution. Thank you.
My case is the next:
One of the rule.
I write tests using testng and three assertion libs are accessible from the tests.
I want to use only Hamcrest assertions, but not testng or assertj.
This is not actually architecture rules but with this, I'd like to help novices in the team and rid of code mess.
from archunit.
For code source approach I look towards https://github.com/forge/roaster
I already use it to extract useful statistic from my test.
from archunit.
Peter, thank you for help and ArchUnit.
from archunit.
No probs 😄 If that solved your problem, is it okay to close the issue?
from archunit.
Yeah, that is solved.
from archunit.
Related Issues (20)
- 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
- Problem with maven multi module project HOT 2
- False positive results when I use ArchRuleDefinition.noClasses() HOT 7
- Check access classes only works for some cases HOT 1
- ArchUnit do not correctly check Annotation on enum Constants HOT 2
- ArchUnit thinks "Switch with arrows" produces non-final fields HOT 1
- Feature suggestion: Expose synchronized blocks in core API
- Performance: archunit-junit5 may scan complete JDK for `@ArchTest`
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.