How to use JUnit5 & Mockito on Eclipse

Daniel S. Blanco
3 min readMar 23, 2021

--

Today we are going to see something very simple, and it is basically how to make JUnit and Mockito work on Eclipse. It is not a great post that brings us or clarifies the specific operation of a technology. But it is a helpful post that can be useful to have at hand to quickly configure our project, especially if we do not have a good memory.

Eclipse is one of the most famous development tools and my favorite, at least when we develop Java applications. It makes the work much easier and in the case of tests, it allows you to run them easily through the context menu. You even can run a single test or all the tests of a certain class or project.

The thing is that with JUnit 5, it is not that it is difficult to make it work but it does require a minimum of configuration.

Before we start let’s see as always a little bit of theory about JUnit 5. It is the sum of three different projects:

  • JUnit Platform: It is the base of the testing Framework. And it contains the testing API that allows us to run the tests.
  • JUnit Jupiter: It is the library that contains the new JUnit 5 programming model and its extension model. It includes annotations such as @Tests, essential.
  • JUnit Vintage: This is the project that allows us to run older versions of JUnit, specifically versions 3 and 4.

In the first step we must configure the dependencies:

Now we only have to prepare for our test so that we can run it. The tests can be executed from Eclipse or with Maven commands. However, to run them from Eclipse it is necessary to use Eclipse Photon or a later one.

Apart from the essential Test annotation on each of the tests we develop. This last annotation is the one that will allow the methods of the class to be taken into account as tests. Without it, the tests will be taken into account by Eclipse but not by Maven.

To be taken into account by Maven we still must include another configuration item. This time, the maven-surefire-plugin, at least its version 2.22.0.

As we have said we can execute the code with the typical maven command:

mvn clean test

Or directly on eclipse through the context menu, if we want to execute only a specific class or method:

Now comes the turn of Mockito, something that is not complicated either. Mockito is a great library that helps us to ‘mock’ concrete objects to, for example, be able to return a concrete response for our object or simulate the response of objects that you would not get otherwise. We can say that it is also a fundamental library if we want a great coverage of our tests.

The first thing we will have to do is to include the Mockito dependency. This framework has a specific dependency for the execution of JUnit 5 tests.

The next step will be to add the ExtendWith annotation to our Test class. This indicates that we are going to use the MockitoExtension that makes use of the JUnit 5 extension model.

As you can see, with 3 dependencies and 1 annotation we can run without problems two of the most important testing frameworks in our favorite IDE.

--

--

No responses yet