Search code examples
javajunitarchunit

Parameterized archunit test?


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.


Solution

  • 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);