Saturday, February 16, 2013

A Beginner's Guide for Learning Java Spring MVC Framework - Part 3


In our last article, we have learned some Java Spring MVC basic concepts and investigated the Java source code from the sample project. Also, two exercises are provided at the end of the article to extend the functionality of the sample project. In this article, we will continue to extend the sample project to include unit test code for the Java code we have written. Unit testing is very important in software development. It allows developers to check the quality of the code. It is a good practice to write unit tests for all the Java classes created in your project. In the test-driven development(TDD) process, developers are even required to write unit tests before writing the actual code.

Unit Test with JUnit


JUnit is widely used as a testing framework for Java code. It is integrated into our implementation IDE, Spring Suite Tools. The sample project also already contains test folders src/test/java and src/test/resources. We can just add files under them. The following steps show how to add a unit test for the controller class HomeController.

Right click the package name under src/test/java, and choose New -> JUnit Test Case. In the pop-up dialog, fill in the test name as HomeControllerTest, check all the method stubs you like to create and select the class under test by click Browse... button. The class we are going to test is com.mycompany.hello.HomeController.



Check the home method to test in the next dialog, and click Finish button.



A skeleton code is automatically generated. In this test file, every method has an annotation before it (a new feature for JUnit 4.x). You can read the following JUnit tutorial  for the detailed information about these annotations. Since our code is pretty simple, we only need to implement the “real” test method, testHome.

In this test method, we first create the HomeController object, then create two objects for the input parameters, locale and model. The model object is an instance of ExtendedModelMap which implements the Model interface. After we call the home method, we test whether the return string is “home” and the model object is modified correctly.

public void testHome() {
            HomeController controller = new HomeController();
            Locale locale = new Locale("en");
            ExtendedModelMap model = new ExtendedModelMap();
            String view = controller.home(locale, model);
            assertEquals(view, "home");
            assertEquals(model.size(), 1);
            assertTrue(model.containsAttribute("serverTime"));
      }

After the code is finished, right click HoemControllerTest.java and select Run As -> JUnit Test. Spring Tool Suite will then run the unit test and show the test results.

Code Coverage

We use unit tests to check the quality of the code. How can we check the quality of the unit test code? One way to measure the quality of the unit tests is to use code coverage, which measures the percentage of the source code that is tested by the unit tests.

We use EclEmma as our code coverage tool. To install EclEmma, open the Help -> Eclipse Marketplace… dialog, and search for EclEmma. In the search results, select EclEmma and then follow the instructions to install it. After the installation, open Window->Customize Perspective… dialog and enable Java Code Coverage in the Command Groups Availability tab.



After we finish adding the code coverage into our IDE, a new code coverage button  is added in the main toolbar.

EclEmma uses a metric called branch coverage to measure the coverage percentage. In general, in order to have a 100% branch coverage on a function, your unit test code must test every statement of the function, and both true and false cases of every conditional statement, such as if, switch, while, ? operator.

To run the unit test with code coverage, you can simply click the “Coverage Run” toolbar button. After the unit test is finished, you can see the results in the “Coverage” tab under the source code editor window. Also the results are intuitively shown in the source code editor window by highlighting code lines with different colors. In default, EclEmma uses green, red, and yellow to indicate different coverage states: fully covered, not covered, and partially covered respectively. A partially covered code line is normally due to the missing of branches. For example, only the true case of an if statement is tested, but the false case is not tested.



Exercise
Write unit tests for the code you added in the previous exercises. Try to get 100% code coverage for your unit test code.

No comments:

Post a Comment