What is correct way writing Integration test in Spring or Spring Boot based application

Viewed 114

I am writing code for Spring Boot Rest application which interact with DB using Spring JPA. My app have 3 main layers Controller,Service,Repository and it have CURD operations.

I want to follow TDD approach. My question is how do I populate data for each VERB implementation.

For example I am starting with CREATE impl and implemented CREATE flow with Controller,Service,Repo etc. Now to implement PUT,GET,DELETE I need to populate data while writing my tests. For this purpose I used Injecting Repository in my Integration test class and loaded data before my actual Test Runs or Used DataLoader with CommandLineRunner Implemention to pre-populate the data. Buy my collegue insisted me I should never use Repository in Integration Test class for populating data instead should User Service class bean and call CREATE implementation for required data population.

Is it any best practice or guideline documentation to design Integration Test and Unit Test?

And main question did we use Repository in Integration Test class for populating data or not?

1 Answers

The main motive of writing integration testing is to test the interface between two software units or modules. It focuses on determining the correctness of the interface. That means you should test your application in the sense of whether your app can be integrated into other software or not. In that case, your beans like repositories or services are not injectable or applicable from the other software except your endpoints that you are exposing through controllers.

Writing Integration Test

There are a couple of things you should consider before writing your integration test such as the scope of your test cases, scenarios of each endpoint, tools/libraries to write the tests, etc.

You can use something like RestTemplate or MockMvc to invoke HTTP requests(POST, PUT, CREATE, DELETE). For example, make a GET request with RestTemplate,

@Autowired
private RestTemplate restTemplate;

@Test
void givenYourObjectTypes_whenGetYourObjectTypes_thenStatus200()

    ResponseEntity<YourObjectType> response = restTemplate.getForEntity(requestUrl, YourObjectType.class);

    assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}

My question is how do I populate data for each VERB implementation.

There are annotations called BeforeEach and BeforeAll, you can use either of them in a setup method to populate your data

Is it any best practice or guideline documentation to design Integration Test and Unit Test?

There are a lot of documentations you can find out in google. But once you grasp the core concept of the test, you will be intimated to workaround. Still, I would refer to you to have a look at an article by Martin Fowler on Integration Test (It's my personal preference).

And the main question did we use Repository in Integration Test class for populating data or not?

Populating data using repositories is not the recommended approach. Instead, I suggest you use CREATE API to populate data which would be a real scenario while integrating with other services/UI/modules/software.


Moreover, You could try H2 dependency with scope test while testing your application which makes it faster to perform the test cases. But note that it is only applicable if you are using SQL database.

Related