Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Eclipse default to JUnit4 rather than JUnit5 when the class only contains JUnit4 annotations

A recent upgrade of groovy which brought with it JUnit5 has resulting in Eclipse wanting to run every test under JUnit5. I can sort of work around this by going to run configurations and tell Eclipse to use JUnit4, however this gets tedious.

Is it possible to tell Eclipse to always use JUnit4 for a particular project, including new tests?

like image 988
Luke Avatar asked Mar 06 '19 21:03

Luke


People also ask

How do I set JUnit to default in Eclipse?

The steps are as below: Project > Properties > Java Build Path > Libraries. Click "Add External JARs..." button at right side --> Select your preferred JUnit jar. Click OK button.

What is the difference between junit5 and junit4?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.

Which annotation is used to call a method before each test case in junit5?

The @Before annotation is used when different test cases share the same logic. The method with the @Before annotation always runs before the execution of each test case.


1 Answers

You can specify a profile and activate it in Eclipse only.

Example (assuming your problem arose by having a dependency to groovy-all):

    <profiles>
        <profile>
            <id>eclipse-groovy-no-junit5</id>
            <dependencyManagement>
                <dependencies>
                    <dependency>
                        <groupId>${groovy.groupId}</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>${groovy.version}</version>
                        <type>pom</type>
                        <exclusions>
                            <exclusion>
                                <groupId>${groovy.groupId}</groupId>
                                <artifactId>groovy-test-junit5</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                </dependencies>
            </dependencyManagement>
        </profile>
    </profiles>

Then go to

Project -> Properties -> Maven -> Active Maven Profiles

and specify the profile name eclipse-groovy-no-junit5.

Remark: groovy.groupId is set to org.codehaus.groovy (it's a variable, because it will be replaced by org.apache.groovy in 4.x) and groovy.version is set to 3.0.8.

If you want to be a bit more verbose, you can add...

    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>

...but it's the default anyway.

like image 79
nineninesevenfour Avatar answered Nov 14 '22 23:11

nineninesevenfour