nicomp
nicomp

Reputation: 4647

How to integrate JUnit 5 with an existing Eclipse project?

The existing Eclipse project uses Maven but does not know about JUnit. Should/can I integrate JUnit into the existing project or should I make a new project dedicated to JUnit or is there a better option?

Upvotes: 4

Views: 5047

Answers (2)

glytching
glytching

Reputation: 47965

You can add JUnit5 to that project by including the following dependencies in the pom.xml:

<properties>
    <junit.jupiter.version>5.0.1</junit.jupiter.version>
    <junit.platform.version>1.0.1</junit.platform.version> 
</properties>

<!--
    JUnit5 dependencies:
     * junit-jupiter-api: for writing JUnit5 tests
     * junit-jupiter-engine: for running JUnit5 tests
     * junit-platform-xxx: the foundation for JUnit5
     * (Optionally) you might want to include junit-vintage-engine for running JUnit4 tests       
-->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>${junit.platform.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>${junit.platform.version}</version>
    <scope>test</scope>
</dependency>

To enable Maven Surefire to run JUnit5 tests just include the following plugin definition in the pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.plugin.version}</version>
    <configuration>
        <excludes>
            <exclude>**/MongoPopulatorTool.java</exclude>
        </excludes>
    </configuration>
    <dependencies>
        <!-- integrates JUnit5 with surefire -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>${junit.platform.version}</version>
        </dependency>
        <!-- ensures that a JUnit5-aware test engine is available on the classpath when running Surefire -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
    </dependencies>
</plugin> 

And, finally, to enable Eclipse's test runner to run JUnit5 tests you must be running Eclipse Oxygen.1a (4.7.1a) release (or later), have a look at the Eclipse docs.

Upvotes: 4

GhostCat
GhostCat

Reputation: 140553

The other answer gives the technical answer how to add JUnit to your project setup.

But sorry, the real answer is: don't add your unit tests to your other project. Create a new one instead.

One of the most important rules when doing development is the Single Responsibility Principle. Any class/method/xyz should be doing one thing.

In other words: your existing eclipse project has the responsibility to provide the context for your "product". Providing context for testing is a different responsibility.

And beyond that, you should always follow "best practices". And best practice is again to not have test and production code within the same project.

You see, you absolutely do not want that your test source code sits in the same directory as your production code. Therefore, you have two projects, that can both use the same packages - but have their source code sitting in different folders!

( the reason why you don't want to have that: you only want to allow your tests to depend from your production code. but when files sit in the same directory, you might inadvertently create dependencies in the other direction )

Upvotes: 2

Related Questions