Replies: 1 comment
-
I'm glad that you like ArchUnit! Here are some ideas how to address your hurdles: (1) static final JavaClasses importedClasses = new ClassFileImporter().// ...
@ParameterizedTest
@MethodSource("domainClasses")
void testMyConditionOnJavaClass(JavaClass javaClass) {
ArchRule rule = classes().should(myCondition());
JavaClasses selectedClass = importedClasses.that(have(name(javaClass.getName())));
rule.check(selectedClass);
} (2) To retrieve individual classes from a given conjunction, I can think of a workaround evaluating a rule where the condition does nothing but dump the objects selected by the given conjunction: static List<JavaClass> domainClasses() {
return extractGivenObjects(domainClasses, importedClasses);
}
static <T> List<T> extractGivenObjects(GivenConjunction<T> conjunction, JavaClasses classes) {
List<T> filteredObjects = new ArrayList<>();
conjunction.should(new ArchCondition<T>("just a hack to retrieve the filtered") {
@Override
public void check(T item, ConditionEvents events) {
filteredObjects.add(item);
}
}).evaluate(classes);
return filteredObjects;
} Does this help? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First: ArchUnit really rocks, great work. Very helpful, so thanks very much for your work! ❤️ 👍
Actually I was coming around the following problem these days. In my tests I want to ensure that some classes (actually domain classes in an onion architecture) fulfill some condition(s).
I have an ArchUnit test which looks like this:
So this works well, no problem so far.
Actually now I came across the idea that perhaps I can rewrite this to a
ParametrizedTest
. So not checking allJavaClasses
from theClassFileImporter
at once but filtering out thedomainClasses
, put them to aStream<JavaClass>
and then check oneJavaClass
after the next. I would appreciate some advice here. Not sure if this is even possible today.I see a bunch of hurdles:
(1) Trivial enough: the
ArchRule
's methodscheck
andevaluate
always getJavaClasses
, not a singleJavaClass
. I have an ugly workaround for this - by reimporting all classes and then filtering only theJavaClass
I want to check (not nice, totally inefficient). Ok, so be it. (I tried also to create aJavaClasses
instance out of theJavaClass
but its constructors are (package) private.)(2) My bigger issue: Ideally, I would like to use the
GivenClassesConjunction
as shown above. Nice API, fluent and verbose methods like for exampleresideInAnyPackage(...)
. Is there a way to create aStream<JavaClass>
out of this? If so, I could write some parametrized test like this (pseudo code):Is something like this possible? What am I missing? Any help and advice very much appreciated! ❤️
PS: I understand, that I could create a Stream like this:
My problem (or lack of understanding?) is here, that
someFiltering
is then aDescribedPredicate<JavaClass>
. Actually I would really like to re-use the fluent API ofGivenClassesConjunction
like for exampleresideInAnyPackage(...)
directly - and not to reimplement its functionality by checking in the predicate'stest
method, if theJavaClass
's package is in any of the packages (including the wildcard functionality you have already implemented).Beta Was this translation helpful? Give feedback.
All reactions