我想编写一个集成测试来接受宁静的请求并通过JPA处理它们
如何在让JPA CRUD成为h2数据库的同时,通过宁静的服务端点集成测试整个应用程序?
类似于以下内容:(从mkyong偷偷偷偷修改)。
我确定其他人一定已经考虑过这个问题,要么这样做,要么有一个更好的方法,要么是不这样做的原因。
@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)
]
如果这是必须具备的功能,您是否研究过使用Selenium Webdriver?它使用实际的应用程序和数据库,因此无需担心使用H2。