Parameterized archunit test?

169 Views Asked by At

I have a list of method names for which I'd like to write a ArchUnit test in the form of:

ArchRule rule = methods().that().haveName("methodName").should(new ArchCondition<JavaMethod>.   ("test") {
    @Override
    public void check(JavaMethod javaMethod, ConditionEvents conditionEvents) {
      ...
    }
  });

  rule.check(classesToTest);
}

}

I tried to use @ParameterizedTest and @ArchUnit in combination, but this didn't work. I also tried creating the ArchRule in a for loop, but this fails when the first violations happens and doesn't check the rules I'm trying to test later.

2

There are 2 best solutions below

0
Oleg On BEST ANSWER

In the meantime I found the solution myself. What you can do in the following case is to create a list of ArchUnit rules:

List<ArchRule> rules = new ArrayList();

for (....) {
   rules.add(...)
}

And then, you can combine them all together and test:

CompositeArchRule.of(rules).check(classesToTest);
0
Manfred On

@ArchTests are evaluated by ArchUnit's own test runner, which does not support JUnit's @ParameterizedTests.

Maybe you can create your ArchRule with a parameterized method that allows you to create

@ArchTest
ArchRule rule1 = createRule(1);

@ArchTest
ArchRule rule2 = createRule(2);

@ArchTest
ArchRule rule3 = createRule(3);

// ...

with just a little overhead?

Alternatively, you can also write ArchUnit tests as plain JUnit tests if you take care of the class file import yourself, cf. https://www.archunit.org/getting-started.