Dmytro Shvechikov
Dmytro Shvechikov

Reputation: 746

Spring Boot Integration Tests With @MockBean don't release jdbc connections

I have a problem with integration tests.

We're using spring boot 1.4.4 + spring batch + testcontainers + postgres.

Each integration test annotated:

@RunWith(SpringRunner.class)
@ActiveProfiles(value = { "integrationtest" })
@SpringBootTest(classes = ServiceApplication.class)

The problem is:

Looks like each integration test that contains @MockBean annotation creates a new context. Each new context creates a new connection pool with a 10 connections. But previous context doesn't release its connections.

So before each such kind of test I can select connections count from postgres:

select sum(numbackends) from pg_stat_database;

And see that each test adds 10 new connections.

10th test fails because of 100 connections limit.

Could you advice how to solve it?

Upvotes: 4

Views: 1419

Answers (1)

Dmytro Shvechikov
Dmytro Shvechikov

Reputation: 746

Looks like a combination of annotations + all @MockBean classes calculates some kind of "hash".

Each "hash" creates its own context and spring caches it. Each contains creates its own connection pool with 10 connections by default.

While all contexts are cached all connection pools hold connections - that's is reason my problem.

As jusermar10 said you can fix it using @DirtiesContext annotation.

Upvotes: 1

Related Questions