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:
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 uses annotations; a few of these are listed in the following table:
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:
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:
Here is the error code in the second case:
@Test public void verifyAddFail() { long sum = simpleCalculator.add(3, 7); assertEquals(11, sum); }