Springboot 2 jpa集成测试是否可以编写为通过rest控制器进行全面测试?

问题描述 投票:0回答:1

我想编写一个集成测试来接受宁静的请求并通过JPA处理它们

  1. 在h2数据库中创建记录
  2. 验证该记录可以通过restful服务(从h2数据库中检索)

如何在让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)
]
spring-boot spring-data-jpa integration-testing spring-restcontroller spring-boot-2
1个回答
0
投票

如果这是必须具备的功能,您是否研究过使用Selenium Webdriver?它使用实际的应用程序和数据库,因此无需担心使用H2。

© www.soinside.com 2019 - 2024. All rights reserved.