Here is a small example that:
- Uses @DataJpaTest annotation to bring in only configuration relevant for JPA tests (entities, repos etc…)
- Uses @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and @Order annotations to enforce order of method execution
- Uses @ComponentScan(basePackages = {“tech.dimitar.rosdjpaint.bootstrap”}) annotation to bring in the component that bootstraps the data
- Uses @Commit annotation on test to enforce transaction does not roll back after test is done (which is the default behavior)
...
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DataJpaTest
@ComponentScan(basePackages = {"tech.dimitar.rosdjpaint.bootstrap"}) //Scan package that contains command line runner that inits the data
public class SpringBootJpaTestSplice {
@Autowired
BookRepository bookRepository;
//@Rollback(value = false)
@Commit
@Order(1)
@Test
void testJpaTestSplice() {
final long countBefore = bookRepository.count();
assertThat(countBefore).isEqualTo(2L);
bookRepository.save(new Book("Test Book", "1234", "Self Publishing"));
final long countAfter = bookRepository.count();
assertThat(countBefore).isLessThan(countAfter);
}
@Order(2)
@Test
void testJpaTestSpliceTransaction() {
final long countBefore = bookRepository.count();
assertThat(countBefore).isEqualTo(3L);
}
}