question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedded mongo start instance for every specification

See original GitHub issue

Hello

Issue

Embedded mongo starts instance for every specification (we use spock in tests). We use spring autoconfig for your project In that case we have issue with performance of our tests, because if we have, for example, 10 specifications, embedded mongo start 10 new instances.

What we do to try to fix it

We try to force embedded mongo to use only one port, by generating it in gradle and set SPRING_DATA_MONGODB_PORT to some free port, but then we got exception about “<EOF>”. What is funny here, first specification working fine, but second and next failed. I checked if this single instance is running and i was able to connect to this mongodb using mongo cli and execute query, insert for example.

Problem starts here: de.flapdoodle.embed.mongo.AbstractMongoProcess -> onAfterProcessStart, there is where problem starts. and exception “<EOF>” thrown from: de.flapdoodle.embed.process.io -> run (finally block)

My first workaround was to start and kill it manually in gradle, to do it globally before all test and after all tests, but for me that was quite “tricky” so i abaddon this idea.

DirtyContext with AFTER_* working also fine (kills mongodb instance after specification), but as above - stupid idea, because it needs to be added in every test.

Conclusion (my own)

My assumptions are that when we refreshing context (not fully recreate context), that is why DirtyContext and manual starting embedded mongo working fine. To be clear - I use the Spring autoconfiguration for embedded mongo, probably using manual starting and killing mongodb would working fine, but here is not my point, to manually workaroud this issue.

Question

Did someone had same issue and have some solution to use only one instance or kill after every test suite instead of instance for every specification?

Example

Stack used here: Spring Boot 2.6.1 Groovy: 3.0.9 Spock: 2.0-groovy-3.0 Embedded mongo: this from spring, 3.0.1 or 3.2.4 (tried them two, same result)

Test classes, examples:

@DataMongoTest
@ContextConfiguration(classes = [FirstConfig, SecondConfig])
@TestPropertySource(properties = [
    "property-a=a",
    "property-b=b"
])
@ActiveProfiles("test")
class FirstTests extends Specification {
}
@DataMongoTest
@ContextConfiguration(classes = [ThirdConfig])
@TestPropertySource(properties = [
    "property-c=c",
    "property-d=d"
])
@ActiveProfiles("test")
class SecondTests extends Specification {
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
czmokucommented, Dec 22, 2021

Sure, no problem here 😃

For other, if had similar problem - currelny we “workaround” this with adding @DirtiesContext annotation with AFTER_CLASS. Now mongo shutdown correctly.

Happy Cristmas!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot Integration Testing with Embedded MongoDB
In this tutorial, we'll learn how to use Flapdoodle's embedded MongoDB solution together with Spring Boot to run MongoDB integration tests ...
Read more >
Using embedded mongo in spring for local testing only
I'm trying to use the flapdoodle embedded mongo database ('de.flapdoodle.embed:de.flapdoodle.embed.mongo') for local manual testing the way ...
Read more >
Organization Flapdoodle OSS | Embedmongo.flapdoodle.de
Embedded MongoDB will provide a platform neutral way for running mongodb in unittests. ... in a Scala/specs2 specification using specs2-embedmongo ...
Read more >
Fast and Stable MongoDB-Based Tests in Spring - nexocode
The library integration with Spring Boot works by starting an embedded MongoDB instance when Spring context is started.
Read more >
Model One-to-Many Relationships with Embedded Documents
Embedding connected data in a single document can reduce the number of read operations required to obtain data. In general, you should structure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found