I would like to write an integration test that accepts restful requests and processes them via JPA
- Create a record in the h2 database
- Validate that record can be retrieved via the restful service (from h2 database)
How can I integration test an entire app via restful service endpoints while letting JPA CRUD an h2 database?
Something similar to the following: (shamelessly stolen and modified from mkyong).
I'm sure others must have considered this and either do it, have a better approach, or a reason for not.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DataJpaTest // This breaks
public class BookRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private BookRepository repository;
@Test
public void testFindByName() {
entityManager.persist(new Book("C++"));
List<Book> books = repository.findByName("C++");
assertEquals(1, books.size());
assertThat(books).extracting(Book::getName).containsOnly("C++");
}
}
java.lang.IllegalStateException:
Configuration error: found multiple declarations of @BootstrapWith for test class
[com.mkyong.BookRepositoryTest]: [
@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)
,
@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper)
]