0

I would like to write an integration test that accepts restful requests and processes them via JPA

  1. Create a record in the h2 database
  2. 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)
]
Jee Mok
  • 6,157
  • 8
  • 47
  • 80
  • (will format properly) To further clarify Kyong has https://github.com/mkyong/spring-boot With https://github.com/mkyong/spring-boot/tree/master/spring-data-jpa-mysql and https://github.com/mkyong/spring-boot/tree/master/spring-rest-hello-world Is it possible to combine these two into a single integration test? – user1825375 Oct 02 '19 at 01:08
  • May be 2 years too late but I had the same problem and found [this article](https://medium.com/qash/integration-testing-a-spring-boot-restful-service-with-an-embedded-test-database-a0006f99b63b) that helped me solving it. – olivergregorius Jan 14 '22 at 23:12

1 Answers1

0

If it is a must have feature, did you look into using Selenium Webdriver? It uses actual application and database so no need to worry about using H2.

H Pat
  • 134
  • 5