javaarchunit

How test our ArchUnit rules?


We have more and more ArchUnit rules and our project evolves. We wrote rules some times ago and in the rule we often have hard code packages or field names.

How can we enforce our rules with tests that guarantee our rules are still valid ? (ie detecting the violations we expect to be detected).

The only way I know is manually changing my code to create the violation and check that the rule fails. But this is not automatically done.


Solution

  • ArchUnit tests can be tested in unit tests. I do that here https://github.com/larrydiamond/ArchitectureUnitTests/blob/main/src/test/java/com/ldiamond/archunittest/TestArchitectureUnitTest.java

    @Test void testNoImplInterfacesDoesNotFailRuleset () { // Negative test for INTERFACES_SHOULD_NOT_END_IN_IMPL
        ArchitectureUnitTest.testArchitecture("com.ldiamond.archunittest.impl.doesNotHaveImplInterface");
    }
    
    @Test void testNoInterfacesDoesNotFailRuleset () { // Positive test for INTERFACES_SHOULD_NOT_END_IN_IMPL
        ArchitectureUnitTest.testArchitecture("com.ldiamond.archunittest.impl.doesNotHaveInterfaces");
    }
    
    package com.ldiamond.archunittest.impl.doesNotHaveImplInterface;
    
    public class BlahImpl implements Blah {
        public String blah () { return "blah"; }
    }
    
    
    public class Blah {
        public String blah () { return "blah"; }
    }
    

    Hope this helps!