Set up unit testing with JUnit

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.

<dependency>
  	<groupId>junit</groupId>
  	<artifactId>junit</artifactId>
  	<version>4.8.1</version>
  	<type>jar</type>
  	<scope>compile</scope>
</dependency>
<dependency>
        <groupId>org.mockito</groupId>
    	<artifactId>mockito-all</artifactId>
    	<version>1.8.5</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>3.0.0.RELEASE</version>
</dependency>

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 GreetingControllerTest.java

In the “Web Components” section we will add Spring and a java class called “GreetingController.java” 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 TestGreetingController.java with the package name com.bitbybit.web.controller

GreetingControllerTest.java
package com.bitbybit.web.controller;
 
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import static org.mockito.Mockito.*;
import com.bitbybit.service.GreetingService;
import junit.framework.TestCase;
 
public class GreetingControllerTest extends TestCase {
 
	@Test
	public void testTheMethodShowAllGreetingsShouldReturnAResult() {
                //GIVEN		
		GreetingService fakeGreetingService = mock(GreetingService.class);
		GreetingController controller = new GreetingController(fakeGreetingService);
		Map<String, Object> model = new HashMap<String, Object>();
                //WHEN
	        String result = controller.showAllGreetings(model);
                //THEN
	        assertNotNull(result);
	        assertEquals("greetings",result);
	}
 
}

Now we have written our first unit test! It is a very basic test, and doesn’t do much for us, but at least we have gotten started with unit testing. (Note that true Test Driven Development means writing the tests first, and then implementing the code! Make sure you do that next time. We’ll see how to write “test first” code soon…)

4. Run the test 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 GreetingControllerTest.java 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!

5. 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

0 Comments

Post a Comment

(required):