Fix onlyxxx that behaving inconsistently for unresolvable targets #348
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So far the set of
should().only{Access/Call}...That...(..)
syntax methods have behaved a little strange when the target of the call was not imported from a class file. This happens for example for unresolvable types like array types.The basic problem is, that from the byte code point of view only certain properties about an
AccessTarget
(likeSomeClass.method()
) can be derived. For further details (e.g. is the method called annotated with a certain annotation) it is necessary to try and resolve the respective method that is targeted from the imported targetJavaClass
. This resolution can unfortunately fail, if the target class has not been imported by ArchUnit (or is unresolvable like an array or primitive type).So far these
only...
methods wanted that at least one of the resolved targets would satisfy the given predicate (e.g.annotatedWith(..)
). If the target is unresolvable this assertion would always fail, reporting things like[some.Array;.clone() is not annotated with ...
. But since it would always fail, it could at the same time complain about[some.Array;.clone() is not annotated with ...
and[some.Array;.clone() is annotated with ...
which is certainly super confusing (and plain wrong in the second case wherenot(annotatedWith(..))
would be used; compare the example).This PR resolves this issue by counting unresolvable targets as always satisfying these predicates. This might lead to wrongly successful tests if classes are missing from the import, but in the end it is indeterminable for ArchUnit anyway, what classes are wanted / desired for the test and which classes are not wanted. It is also a fair point of view to say that unimported classes should not be considered when asserting
should().only...
.Resolves: #340