5 min Read.

Hola !

This post is for brushing up our skills on Testing framework, first we will scratch the surface of testing by understanding the basic differences between Unit and Integration. Followed by examples on both, we will also look at the annotations which come handy while writing test cases. Code base link 

Unit Testing

Unit testing as the name suggests here we test the individual part of our software. Unit Testing is carried out ideally during the development phase of SDLC. Individual parts can be individual function/method or procedure. Unit Testing is done typically by developer. This process helps the developer to identify any missed functionality in a method/function.

Integration Testing

Integration Testing is process of testing interfaces between two or more modules. The purpose of Integration testing is to check the correctness of interaction between two or more interfaces. In Integration Testing modules can be different layers in our code or can be entirely different services. Ideally Integration Testing is always done  after Unit Testing.

Bulleted Differences :

Unit Testing Integration Testing
Each module is tested separately All modules are tested combined
Basically done by Developer who is
aware of internal design also known as White Box Testing
Done by Tester, who is
not aware of internal design also known as Black Box Testing
Performed first step of all testing processes Done after Unit testing and before System Testing
Detection of defects is easy Detection of defects is difficult
Less Cost High Cost

Testing Flow : Unit Testing

Example of Unit Testing  :

Code Snippet 1

In Code snippet 1, we have a class which contains the method multiply, we will use assertEquals(int expected, int actual) which is pretty basic stuff for experienced developers. Let’s have a look at unit test cases in real scenarios using Mockito in our Spring boot application, used in our day to day development

Example of Integration Testing  :

Code Snippet 2  : Testing MVC/Controller Layer

Above code snippet 2 is for testing Spring Controller layer, we can observe few annotations and methods, please read on for more description:

@WebMvcTest(StudentController.class) used for mocking the HTTP methods present in StudentController.class

@RunWith(SpringRunner.class) SpringRunner is an alias for SpringJUnit4ClassRunner which joins the JUnit with Spring TestContext framework, with SpringRunner we can implement simple JUnit based Unit and Integration Tests

andDo(), allows us to perform general operation like print the request and response bodies

andExpect(), allows us to perform testing the expected outcomes

anyString(), used for mocking the behaviour of any argument for the given type here we are using anyString(), we also have anyInt(), any(Object.class) as viable options

Code Snippet 3 : Integration Test for Spring Boot Application

In above code snippet 3 : we can observe a new annotation @SpringBootTest, this annotation allows us to spin up the whole application, creating real objects, connecting to real database(can be InMemory database) or calling other services or connecting to third Party APIs, in other words trying to startup the application just for testing purposes. It is also known as Black Box Testing we don’t mock any thing in this type of testing every object is real.

Big question when we should we use @RunWith(SpringRunner.class)  and @SpringBootTest annotations or both?

When we want to test a method under our Spring Boot Application mocking other objects of application like database connection/transactions or 3rd Party APIs we use @RunWith(SpringRunner.class) or @RunWith(MockitoJUnitRunner.class) and when we want to connect to real API or databases based on the real configurations we use @SpringBootTest annotations.

Thanks for reading this blog, I hope it was useful !

For more detailed information please reach out to me at atish.verma@imaginea.com