In my previous blog post I have created Spring Boot REST API for simple CRUD operation demonstration. In this post I'm demonstrating how to use JUnit testings in the REST API.
Prerequisites
- You should have install java 1.8 or above.
- You should have Eclipse installed in your PC.
- Your PC should setup Maven installed and configured.
- You should have REST API developed for testing I'm using REST api which I developed on previous post. (You can checkout previous code from here)
Spring Boot dependency
First of all lets add the dependency in to POM file related to the JUnit test. All required dependencies comes under the spring-boot-starter-test. Just need to add following part on the existing pom file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Test Class creation
Lets create "TestRestAPI" under the src/test/java folder.
In the class level we need to have following annotations
@RunWith(SpringJUnit4ClassRunner.class)
Indicates that the class should use Spring's JUnit facilities
@SpringBootTest(classes = AppInitializer.class)
Specify the springboot initializing class
@WebAppConfiguration
It’s used to denote that the ApplicationContext which is bootstrapped for the test should be an instance of WebApplicationContext.
In the class level we need to have following annotations
@RunWith(SpringJUnit4ClassRunner.class)
Indicates that the class should use Spring's JUnit facilities
@SpringBootTest(classes = AppInitializer.class)
Specify the springboot initializing class
@WebAppConfiguration
It’s used to denote that the ApplicationContext which is bootstrapped for the test should be an instance of WebApplicationContext.
Then we have @Before method with following content. This will initialize our MockMvc variable using application context.
@Before
public void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
Whole TestRestAPI class implementation can be see on following github location.
In that Test class I have added following methods in order to test deferent scenarios in the application.
bookEqualsMethodTest
This method will test whether our equals method on Book model class work as expected.
searchNonExistingBook
This method will test whether correct out put return when searching not existence book searched on the system.
saveBookWithoutMandotoryFieldIsbn
This method will test whether correct output return when ISBN number empty or null when saving Book.
saveBookWithoutMandotoryFieldName
This method will test whether correct output return when book name empty or null when saving Book.
saveBookWithoutCorrectBookDetails
This method will test whether correct output return when correct book saved on the system.
saveBookWithoutCorrectBookDetailsAndDeleteWithNullIsbm
This method will test the expected output when try to perform delete operation with null ISBM number.
saveBookWithoutCorrectBookDetailsAndDelete
This method will test the expected output when try to perform delete operation with correct ISBM number.
In order to run the JUnit Test you can right click on the "TestRestAPI" and click on Run As -> JUnit Test as show on bellow.
Then on the result can be seen on the JUnit section.
Also there is another feature where we can see the code coverage of the unit test. In order to see the code coverage we have to run unit test with code coverage. In order to run it you can go to Run menu bar and click on coverage.
Then on the coverage area you can see the Unit Test coverage and also if you click on specific class on the coverage area you can see the areas which has not been tested on the unit test. As show on bellow figure.
You can download whole project with JUnit test in following git hub location.
No comments:
Post a Comment