I'm Using Junit in Spring Boot, along with TestContainers (Docker, MySQL 8.0.29) to develop integration tests.
When I execute my tests individually, they all succeed. However when I run them all at once (i.e. in CI/CD), they fail. This is because the tests are not executed in order, and an item might already be deleted before the test to find the item is executed.
To fix this I want to give the entities a unique ID. However, the ID is already automaticly set in my Hibernate entity:
@Entity
public class Assignment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
I've tried to delete all items before each test is executed, however this does not work:
@Autowired
private JdbcTemplate jdbcTemplate;
@BeforeEach
void tearDown() {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "assignment");
}
Example integration test:
@Test
void When_getById_Verify_Fields() {
AssignmentDTO assignmentDTO = new AssignmentDTO();
assignmentDTO.setTitle("test");
assignmentDTO.setDescription("test");
assignmentDTO.setUserId("1");
assignmentDTO.setCreator("1");
assignmentService.addAssignment(assignmentDTO);
AssignmentDTO expectedAssignment = assignmentService.getById(1);
assertEquals(assignmentDTO.getTitle(), expectedAssignment.getTitle());
assertEquals(assignmentDTO.getDescription(), expectedAssignment.getDescription());
assertEquals(assignmentDTO.getUserId(), expectedAssignment.getUserId());
assertEquals(assignmentDTO.getCreator(), expectedAssignment.getCreator());
}