4

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?

glytching
  • 44,936
  • 9
  • 114
  • 120
nicomp
  • 4,344
  • 4
  • 27
  • 60

2 Answers2

4

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.

glytching
  • 44,936
  • 9
  • 114
  • 120
  • 1
    Whilst technically correct, I think you are wrong on the concept level: you do **not** add test code to the "primary" project. You always separate such things into **disjunct different** projects. – GhostCat Mar 02 '18 at 08:13
  • 1
    I think you can have test code in the same project as the code it is testing albeit in a different code tree (`src/test/` vs. `src/main`). – glytching Mar 02 '18 at 08:20
  • The fact that you technically can do that doesn't mean that you should be doing that. The *project* definition(s) should be separated. – GhostCat Mar 02 '18 at 08:22
  • 1
    The platform artifacts should be version 1.2.0? No such 5.0.1 on maven central. – dan carter Jun 04 '18 at 06:22
  • @dancarter yes, thanks, I have corrected the answer. – glytching Jun 04 '18 at 07:39
  • **Update:** As of JUnit 5.4.0, the Maven artifact [*junit-jupiter*](https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter) conveniently provides the content of 3 other artifacts: `junit-jupiter-api`, `junit-jupiter-params`, and `junit-jupiter-engine`. Discussed in [my Answer](https://stackoverflow.com/a/55084036/642706) on another Question. – Basil Bourque Jun 03 '20 at 01:52
2

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 )

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 4
    In maven unit tests go in the same project as the code under test, but in a different directory: src/test/java – dan carter Jun 04 '18 at 06:16
  • Eclipse meanwhile supports classpath separation. You can mark source folders as "test only", making it impossible to accidentally reference them in your production code. Therefore the strict separation of production code projects and test code projects/fragments is no longer needed in Eclipse. And that's good because test fragments create additional problems (like not being able to depend on another fragment with shared testing utilities). – Bananeweizen May 24 '20 at 06:05
  • The question is whether the standard build tools support that concept. What some IDE does or does not isn't what matters. Or at least it should not. – GhostCat May 24 '20 at 09:13