Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 Console in Spring Boot Integration Test

I'm working with Spring Boot 1.3.1.RELEASE. I enabled the H2 web console by setting spring.h2.console.enabled=true in the application.properties file. If I launch my Spring Boot application I can access the H2 Web console via http://localhost:8080/h2-console/.

However, I am not able to access the console when I perform an integration test in debug mode, where I have set a breakpoint. Unfortunately, this is not working (site is not available). The integration test is configured as follows:

@ActiveProfiles("local,unittest")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebAppConfiguration
@IntegrationTest
public class MyResourceTest {

What I have not considered?

like image 901
René Winkler Avatar asked Feb 02 '16 20:02

René Winkler


People also ask

How to do integration testing in Spring Boot?

The application shall run in ApplicationContext and run tests in it. Spring boot provides @SpringBootTest annotation which starts the embedded server, creates a web environment and then enables @Test methods to do integration testing.

How to enable H2 console in Spring Boot?

H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. To enable it, we need to add the following property to application.properties:

What is @springboottest annotation in Spring Boot?

Spring-Boot provides an @SpringBootTest annotation which provides spring-boot features over and above of the spring-test module. This annotation works by creating the ApplicationContext used in our tests through SpringApplication. It starts the embedded server, creates a web environment and then enables @Test methods to do integration testing.

How does @springboottest start the embedded server?

This annotation works by creating the ApplicationContext used in our tests through SpringApplication. It starts the embedded server, creates a web environment and then enables @Test methods to do integration testing. By default, @SpringBootTest does not start a server. We need to add attribute webEnvironment to further refine how your tests run.


1 Answers

To enable the H2 Web console, you should agregate the value of your properties file related to this item as optional element of the annotation @IntegrationTest.

String[]value Properties in form key=value that should be added to the Spring Environment before the test runs.

Example:

@IntegrationTest({"spring.h2.console.enabled=true"})

And keep in mind this recomendation :

If your test also uses @WebAppConfiguration consider using the @WebIntegrationTest instead of @IntegrationTest

So, try to use @WebIntegrationTest instead of @IntegrationTest and add the key spring.h2.console.enabled = true

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/IntegrationTest.html

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/WebIntegrationTest.html

Maybe you could need add the key "server.port=8080" as another value for the annotation @WebIntegrationTest

like image 62
Jesus Zavarce Avatar answered Sep 21 '22 23:09

Jesus Zavarce