Set up unit testing with JUnit (in the beginning)

1. Add the testing dependencies to your pom.xml

To run junit with annotations, you will need at least junit 4. And, since we will be using Mockito and some of Spring’s testing features as well, you will need to add those dependencies so that you have them for the next steps.


Don’t forget to use Maven to clean the project, and to refresh the project in Eclipse.

2. Create a test source folder called src/test/java

Now, in Eclipse, right click on the project SpringGreetings and select New -> Source Folder. Leave the defaults and enter “src/test/java” for the folder name.

For a better understanding of maven’s standard directory layout, look at this web page for an understanding.

3. Create a java class called

In the “Web Components” section we will add Spring and a java class called “” which contains a handler method responsible for showing a list of greetings. Even though we haven’t done that yet, we will begin to write a test for it already.

Inside our new test source folder, create a java class called with the package name com.bitbybit.web.controller
package com.bitbybit.web.controller;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
public class GreetingControllerTest {
	public void showAllGreetingsMethodShouldReturnAResult() {
	    GreetingController controller = new GreetingController();
	    String result = controller.showAllGreetings();

Now this is a very trivial example. It is just the very first test method, and we won’t even be able to run the test because the GreetingController class does not exist yet. We don’t know much about the application yet, just that we will have at least one method for displaying the greetings and that this method will return a result which will resolve the view. Now our next task is to write the minimal code to make this test work.

4. Create a main source folder called src/main/java

in Eclipse, right click on the project SpringGreetings and select New -> Source Folder. Leave the defaults and enter “src/main/java” for the folder name.

5. Add a java class with the necessary code to make the test compile and pass.
package com.bitbybit.web.controller;
public class GreetingController {
	public String showAllGreetings() {
		return "some result";

Note that we still have not added Spring yet to this application but it does not matter since we don’t need anything Spring specific yet. Generally it is not necessary to use Spring for unit testing, although Spring has several utilities to make testing easier. We’ll get to that later.

This is what your Eclipse directory should look like:

6. Run the tests in your Eclipse IDE.

It’s possible to run unit tests in Eclipse as well as in Maven. It’s much easier and faster to run the tests from Eclipse but we will try out both ways.

You can run tests via Eclipse by right clicking on the test class and selecting
Run as… unit test

If your test configuration is still set up to run Junit 3, then right click on the test class to open the run/debug settings and select Junit 4.

The bar should be green if everything was successful, and red if it failed!

7. Run the tests from Maven. (optional)

You can run the tests in maven by issuing the following:

$ cd SpringGreetings
$ mvn test

Maven looks through all your files to find the unit tests. No need to tell it where they are!

NOTE if you have multiple tests, you can run the test as a single test like this:
$ mvn -Dtest=GreetingControllerTest test

NOTE that tests will also be run when you deploy the application to tomcat.
you can skip the tests when deploying like this:
$ mvn tomcat:redeploy -Dmaven.test.skip=true


Post a Comment