Comments (6)
I think I found the problem, and your observation about Exception
vs RuntimeException
is close 😉
The reason is, that ArchUnit is missing some classes in the import to make the determination. You import all classes in the package of TestAggregate
, these are three classes. For all references outside of the import, ArchUnit creates a stub, i.e. ArchUnit sees, that ForbiddenException
inherits from RuntimeException
, however, RuntimeException
is not part of the import. Since the information is still important, ArchUnit will create a simple JavaClass
with everything that can be determined, which is pretty much only "it's a class and it's called RuntimeException". However, ArchUnit has no way by default, to know that RuntimeException
extends from Exception
, thus your Example fails, but works, if you replace Exception
by RuntimeException
. (I hope I'm making sense...)
There are two ways, to get what you want:
- Also import
RuntimeException
, so ArchUnit knows it's extending fromException
- Change
archunit.properties
to resolve missing dependencies via Reflection (this only works, if the classes are on the classpath, and it's slightly slower, that's why it's disabled by default).
To do the second, you could create a file archunit.properties
within src/test/resources
and set the property
resolveMissingDependenciesFromClassPath=true
In both cases your example behaves as expected when I tested it, as far as I can see.
Sorry, I know, this is a little tricky (I've had the same confusion in the past).
from archunit.
This is weird, because I've added a simple example, a class ValueObject
, a class Birthdate extends ValueObject
with field Date birthdate
, I've added your method Birthdate#hasAgeRequirement(int)
accessing field birthdate
, also added a class Entity
and MyEntity extends Entity
accessing Birthdate
, and a class Evil
not extending anything and accessing Birthdate
. In my example, only Evil
is reported as violation, Birthdate
's self-access is okay, and so is the access from MyEntity
.
You are right, that the behavior you're reporting is not the expected one 😉
However, I would need some minimal example reproducing this, to understand, what's going on (the logic with andShould()
and orShould()
is not trivial, since each ArchCondition
can add various 'events' which then are ANDed, ORed or negated).
Could you try to create a minimal example showing the expected behavior? You could add it to archunit-example
and create a PR, if you want, so I can look at it.
from archunit.
Thanks for the support, I'll investigate this again once I have time.
from archunit.
I created a simplest possible example in this Repository: https://github.com/SteluHH/ArchUnitQuestionExample
Can you have a look? I am not sure if I maybe also get the whole thing wrong, however one of the 2 tests does not fail for me, however I think it should. Note that the test above fails (as expected).
Interesting fact: When I replace Exception.class
with RuntimeException.class
it works:
from archunit.
Thanks @codecholeric for the patient explanation, even though I apparently missed to read the manual :-)
Now I got it, thanks!
from archunit.
As I said, I know the manual 😉, but even I was confused at some point.
Anyway, glad that your example works and that it was no bug 😅
I'll close this issue.
from archunit.
Related Issues (20)
- 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 2
- Feature suggestion: Expose synchronized blocks in core API
- Performance: archunit-junit5 may scan complete JDK for `@ArchTest` HOT 4
- SimpleName-Check failing because of trailing file extension (Kt) HOT 8
- ArchUnits - get name of lambda method HOT 1
- How to fail tests if no classes are imported? HOT 1
- Configuring a Custom gradle Plugin to Run on Client Project HOT 3
- Tests fail when using non-default "target" folder HOT 1
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.