'Are the individual tests in JUnit impacted by the results of prior tests when executing Integration Test using @SpringBootTest
I have a query here for which I was not able to find a resolution despite search for a while on the net and here at StackOverflow.
I wish to check if the results of each individual JUnit test have any impact on the subsequent JUnit tests which are being run as part of the overall Integration Testing.
Below, I have listed the main test methods which are executed and for which I see the following error message surfacing:
[ERROR] Failures: [ERROR]
StudentIntegrationTest.testCreateStudentFailure:77 Status expected:<400> but was:<200>
@SpringBootTest
@AutoConfigureMockMvc
public class StudentIntegrationTest {
@Autowired
MockMvc mockMvc;
@Autowired
ObjectMapper mapper;
@Test
public void testGetStudents() throws Exception{
this.mockMvc.perform(MockMvcRequestBuilders
.get("/api/v1/student")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$",notNullValue()))
.andExpect(jsonPath("$",hasSize(2)))
.andExpect(jsonPath("$[0].name",is("tom")));
}
@Test
public void testCreateStudentFailure() throws Exception{
Student NEW_RECORD=new Student("tom","[email protected]",LocalDate.of(1990, 3, 8));
this.mockMvc.perform(MockMvcRequestBuilders
.post("/api/v1/student")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(this.mapper.writeValueAsString(NEW_RECORD)))
.andExpect(status().isBadRequest())
.andExpect(result->
assertTrue(result.getResolvedException() instanceof IllegalStateException))
.andExpect(result ->
assertEquals("email taken",result.getResolvedException().getMessage()));
}
@Test
public void testDeleteStudentSuccess() throws Exception{
Long idToDelete= Long.valueOf(1);
this.mockMvc.perform(MockMvcRequestBuilders
.delete("/api/v1/student/"+Long.toString(idToDelete))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
To give some background, I have initialized the server such that the following data will be automatically added to the database upon starting the server:
@Configuration
public class StudentConfig {
@Bean
CommandLineRunner commandLineRunner(StudentRepository repository){
return args -> {
Student tom=
new Student(
"tom",
"[email protected]",
LocalDate.of(2000,Month.JANUARY,5)
);
Student jelly=
new Student(
"jelly",
"[email protected]",
LocalDate.of(2010,Month.JANUARY,5)
);
repository.saveAll(
List.of(tom,jelly)
);
};
}
}
This means that the id of 'tom' is 1 and the id of 'jelly' is 2.
However, I do not have such an error when I remove testDeleteStudentSuccess() and run the testing with testGetStudents() and testCreateStudentFailure() executed together.
Hence, I believe that the issue arises because since JUnit can run the tests in any order, when testDeleteStudentSuccess() is executed and the data of 'tom' is deleted, then you will get the following error message when testCreateStudentFailure() is executed because since 'tom' is already deleted from the database hence it should be a success, not a failure:
[ERROR] Failures: [ERROR]
StudentIntegrationTest.testCreateStudentFailure:77 Status expected:<400> but was:<200>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
