Search This Blog

Sunday 19 April 2020

Junit-Eclipse NoClassDefFoundError for LauncherFactory

Issue

Tests are written in junit5. Even when Run As-> 'Run Configurations' -> "Test runner:" = junits5, eclipse not able to run the tests with junit5 runner.

Root Cause

Eclipse Profile for junit5 not configured in core/pom.xml.

Resolution

The key is to add and use a maven profile to solve the Eclipse Case.

It assumes you have defined a property junit5.version in your parent pom like:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit5.version>5.1.1</junit5.version>
</properties>

then in the profiles section add the following:

<profiles>
    <profile>
        <id>eclipse</id>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-launcher</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit5.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-launcher</artifactId>
                    <version>1.1.1</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </profile>
</profiles>



All you have to do after this is to select the profile in your local Eclipse: Right click on your project and select Maven > Select Maven Profiles... (or hit Ctrl + Alt + P), and then check the "eclipse" profile we just created.


With that you are done. Your Eclipse will run Junit 5 tests as expected, but the configuration you added won't pollute other builds or other IDE

Junit-Eclipse No tests found using JUnit 5 or junit4

Issue
Pop up in eclipse saying junit4 tests not found. Not an issue with maven.




Root Cause
Run As-> 'Run Configurations' -> "Test runner:" = junits5 while the tests are written with junit4 api’s.

Resolution
Fixed the issue by right clicking the test and selecting 'Run Configurations' and changing the "Test runner:" selection to 'JUnit 4' as shown here:




The issue can also occur if it’s the other way-round also.