Home > OS >  IntelliJ can't find tests if package name exists in src/main/java
IntelliJ can't find tests if package name exists in src/main/java

Time:01-31

I'm trying to introduce Unit tests to our system, and have run into a problem with Junit not finding test. I have these 3 tests:

enter image description here

When I run all tests in the module:

enter image description here

It finds X and Y tests, but not Z:

enter image description here

The difference between the 3 is only in the package name:

  • The package com.exlibris.x (XTest) doesn't exist in the project
  • The package com.exlibris.core.infra.svc.api.flags (YTest) exists in a different module in the project (that is outputted to a different jar file)
  • The package com.exlibris.repository.web.mms.publishing (ZTest) exists in the same module under the src/main/java

My pom.xml has the following dependencies (inherited from the parent pom):

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.9.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-engine</artifactId>
    <version>1.9.1</version>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
</dependency>

EDIT: These are my run configurations

enter image description here

enter image description here

CodePudding user response:

So it turned out there were a few issues (some of them are likely not related to the specific question, but still best practices). What helped for me was:

  1. Consolidate dependency management of JUnit by importing junit-bom
  2. Upgrading maven-surefire-plugin to latest version
  3. Removing unnecessary configs from maven-surefire-plugin (my configuration tag is now empty)
  4. Reloading the project in IntelliJ (Right click project -> Maven -> Reload project)

Thanks a lot to khmarbaise and Kathrin Geilmann for the help :)

  • Related