Chapter 6. Spring Testing

In software development, testing is a crucial part. Software development cannot be completed without testing. Testing is a process that ensures the performance and quality of software development, and verifies that the applications run smoothly and flawlessly. For this, unit testing is the easiest technique. It allows us to test each component of the application separately. Integration testing ensures that multiple components are working well in a system.

To avoid the mixing of the test code with the normal code, usually unit tests are created in a separate source folder or a separate project. Some developers, on the hot topic, "What should be tested", hold that every statement in the code should be tested.

Testing can be done either automatically or manually, and automated tests can be run continuously and repeatedly at different phases of the software development process. This is highly recommended for the Agile development process. Since the Spring Framework is an Agile framework, it supports these kinds of processes.

The Java platform supports many testing frameworks, in which JUnit and TestNG are the most popular frameworks. In this chapter, we will discuss a popular Java testing framework and the basic techniques of testing. We will also discuss the support provided by the Spring Framework for unit and integrating testing.

Here is the list of topics that will be covered in this chapter:

  • Testing using JUnit 4
  • Testing using TestNG
  • Agile software testing
  • Spring MVC test framework

Testing using JUnit 4

JUnit 4 is the most widely accepted unit testing framework on the Java platform. It allows you to annotate the methods that need to be tested by using the @Test annotation, and it is used to create automated tests for your Java application, which can be run repeatedly to ensure the correctness of your application. The website for JUnit is http://junit.org/.

A Test class contains the JUnit tests. These are methods and are only used for testing. A test method needs to be annotated with the @org.junit.Test annotation. In this test method, you use a method provided by the JUnit framework to check the actual result versus the expected result of the code execution.

JUnit 4 annotations

JUnit 4 uses annotations; a few of these are listed in the following table:

Annotation

Import

Description

@Test

org.junit.Test

The @Test annotation identifies the test cases. A public void method annotated with the @org.junit.Test annotation can be run as a test case.

@Before

org.junit.Before

A public void method annotated with the @Before annotation is executed before each Test method in that class execute. It may be used to set up an environment variable.

@After

org.junit.After

A public void method annotated with the @After annotation is executed after each Test method in that class execute. It may be used to release the external resource that was allocated in a Before method or clean up the test environment and save memory.

@BeforeClass

org.junit.BeforeClass

A public static void method annotated with the @BeforeClas s annotation is executed once, before all the tests in that class are executed.

@AfterClass

org.junit.AfterClass

A public static void method annotated with the @AfterClass annotation is executed once, after all the test methods in that class have been executed. It can be used to perform some clean-up activities, such as disconnect from the database.

@Ignore

org.junit.Ignore

A method annotated with the @Ignor e annotation will not be executed.

Assert methods

JUnit provides the static assert methods declared in the org.junit.Assert class to test for certain conditions. An assert method starts with assert, and then compares the expected value with the actual value returned by a test. The Assert class provides a set of assertion methods of the return type void. These are useful for writing tests. A few of these are listed in the table shown here:

Method

Description

assertTrue(boolean expected, boolean actual)

This method checks whether the Boolean condition is true

assertFalse(boolean condition)

This method checks whether the Boolean condition is false

assertEquals(boolean expected, expected, actual)

This method compares the equality of any two objects using the equals() method

assertEquals(boolean expected, expected, actual, tolerance)

This method compares either the float or the double values and tolerance defines number of the decimal that must be the same

assertNull(Object object)

This method tests whether a single object is null

assertNotNull(Object object)

This method tests that a single object is not null

assertSame(Object object1, Object object2)

This method tests whether two objects refer to the same object, and it must be exactly the same object pointed to

assertNotSame(Object object1, Object object2)

This method tests if two objects do not refer to the same object

An example of JUnit 4

Suppose we are going to develop a simple calculator. We have to test it in order to ensure the system's quality. Let's consider a simple calculator whose interface is defined as follows:

package org.packt.Spring.chapter9.SpringTesting.Calculator;

public interface SimpleCalculator {

   public long add(int a, int b);

}

Now, we can implement this SimpleCalculator:

package org.packt.Spring.chapter9.SpringTesting.Calculator;

public class SimpleCalculatorImpl implements SimpleCalculator {

   public long add(int a, int b) {
          return a + b;
   }
}

Next, we will test this SimpleCalculator with JUnit 4. Most of the IDEs, such as Eclipse, STS, and NetBeans support the creation of the JUnit tests through wizards. Add JUnit 4 JAR to your CLASSPATH to compile and run the test cases created for JUnit 4, as shown here:

package org.packt.Spring.chapter9.SpringTesting.Calculator;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class SimpleCalculatorJUnit4Tests {

   private SimpleCalculator simpleCalculator;

   @Before
   public void init() {
          simpleCalculator = new SimpleCalculatorImpl();
   }

   @Test
   public void verifyAdd() {
          long sum = simpleCalculator.add(3, 7);
           assertEquals(10, sum);
   }
}

Now we can run our test case by right-clicking on the test, and then choosing Run As | JUnit test, and we can verify the JUnit view as the test case should run successfully, as shown in the following two cases:

  • It will display a green bar if the test case passes:
    An example of JUnit 4
  • It will display a red bar if the test case fails:
    An example of JUnit 4

Here is the error code in the second case:

@Test
   public void verifyAddFail() {
          long sum = simpleCalculator.add(3, 7);
          assertEquals(11, sum);
   }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.252.81