No tests were found when running Archunit tests using JUnit5

503 Views Asked by At

I've a very simple Archunit test case running in IntelliJ IDE with Java 17 and JUnit 5. I've added the following dependency in pom.xml:

            <dependency>
                <groupId>com.tngtech.archunit</groupId>
                <artifactId>archunit-junit5-api</artifactId>
                <version>1.1.0</version>
                <scope>test</scope>
            </dependency>

Below is my test class:

    import com.tngtech.archunit.junit.*;
    import com.tngtech.archunit.lang.ArchRule;
    import org.springframework.stereotype.Controller;
    
    import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;
    import static com.tngtech.archunit.core.importer.ImportOption.*;

        @AnalyzeClasses(
            packages = "com.my.package.name",
            importOptions = {DoNotIncludeTests.class})
        public class ArchitectureTests {
        
          @ArchTest
          public static final ArchRule testControllerNaming =
              classes()
                  .that()
                  .areAnnotatedWith(Controller.class)
                  .should()
                  .haveSimpleNameEndingWith("Controller");
        }

When I run the test case, I keep getting "No tests were found". What am I missing?

EDIT 1: I got a part of it working using JUnit annotations. The code below works:

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.Test;
import org.springframework.stereotype.Controller;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;

public class ArchitectureTests {
  JavaClasses jc = new ClassFileImporter().importPackages("com.my.package.name");

  public static final ArchRule testControllerNaming =
      classes()
          .that()
          .areAnnotatedWith(Controller.class)
          .should()
          .haveSimpleNameEndingWith("Controller")
          .allowEmptyShould(true);

  @Test
  public void testControllerName() {
    testControllerNaming.check(jc);
  }
}

However, this only works when run directly from the IDE. Maven doesn't seem to find these tests. Furthermore, the @ArchTest annotation doesn't seem to work. I can only run the method-based tests annotated with @Test. I can't run any field-based test directly annotated with @ArchTest. I compared my code to the examples listed in the official Github repo of ArchUnit and I can't seem to find any difference that may lead me to find any reason for my code not working. I'd appreciate any pointers.

1

There are 1 best solutions below

1
Vimugdh Lall On

As @Manfred hinted, the problem was with my pom.xml. When I didn't get the expected results at the first go, I started adding a bunch of dependencies that were probably not needed and maybe they were conflicting with each other. I had the following dependencies added:

  1. archunit
  2. archunit-junit5
  3. archunit-junit5-api
  4. archunit-junit5-engine
  5. junit-platform-engine (explicitly added when the test cases were not being identified by maven)

I removed everything and just kept archunit-junit5=1.1.0. It just worked out. Below is the working test case that works both when run directly from the IDE or maven - as expected.

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import org.springframework.stereotype.Controller;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;
import static com.tngtech.archunit.core.importer.ImportOption.*;

@AnalyzeClasses(
    packages = "com.my.package.name",
    importOptions = {DoNotIncludeTests.class})
public class ArchitectureTests {
  @ArchTest
  public static final ArchRule testControllerNaming =
      classes()
          .that()
          .areAnnotatedWith(Controller.class)
          .should()
          .haveSimpleNameEndingWith("Controller");
}