1

I'm writing tests for my repository in SpringBoot application. On production I have working MySql DB, but for testing purposes I'm trying to use H2 in-memory DB. It appears to work correctly - there are SQL queries flying in console.

The question is how I can access H2 console page while testing application. When I set a breakpoint in test method I can't access console in browser.

I tried setting up breakpoints that suspends the test thread only (no luck).

When I switch my production DB from MySql to H2, I'm able to access the console, everything works as I suspect it should. No break point is needed in opposite to tests - 'normal' application runs endlessly waiting for HTTP requests.

Simple test class I use:

@RunWith(SpringRunner.class)
@SpringBootTest()
public class StockRepositoryTest {
    @Autowired
    private StockRepository stockRepository;

    @Test
    public void Should_persist_deviceItem(){
        DeviceItem deviceItem = SampleStock.s1_d1;
        stockRepository.save(deviceItem);
        Optional<DeviceItem> foundById = stockRepository.findById(SampleStock.s1_d1.getId());

        Assert.assertTrue(foundById.isPresent());
    }

I expect that when I put a break point on line in test method, while the code is suspended, I can access H2 database console and check state of data. Right now I'm not getting any response on H2 console URL (not even HTTP error code).

Thanks, regards Peter

piotras256
  • 51
  • 3
  • Hey Peter, if you use the spring tools your expectation is wrong. The h2 console is served by your application (localhost:/h2-console). But if you set a break point while the application of yours runs, then you are blocked until the previous part is done. – Eirini Graonidou Jul 06 '19 at 08:13
  • Please show your `application.properties` or `application.yaml` file and the URL you think shold work for the console. There's also a chance you miss a dependendy, so your `pom.xml` could be heplfull as well. – BetaRide Jul 06 '19 at 10:12
  • 1
    @piotras256, Have you tried to set your breakpoint Suspend config to "Thread" instead of "All"? – Gabriel Dec 15 '20 at 19:42

0 Answers0