Git Product home page Git Product logo

Comments (6)

codecholeric avatar codecholeric commented on September 25, 2024 1

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 from Exception
  • 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.

codecholeric avatar codecholeric commented on September 25, 2024

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.

stefluhh avatar stefluhh commented on September 25, 2024

Thanks for the support, I'll investigate this again once I have time.

from archunit.

stefluhh avatar stefluhh commented on September 25, 2024

Hi @codecholeric

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).

image

Interesting fact: When I replace Exception.class with RuntimeException.class it works:
image

from archunit.

stefluhh avatar stefluhh commented on September 25, 2024

Thanks @codecholeric for the patient explanation, even though I apparently missed to read the manual :-)
Now I got it, thanks!

from archunit.

codecholeric avatar codecholeric commented on September 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.