Embedded mongo start instance for every specification
See original GitHub issueHello
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:
- Created 2 years ago
- Comments:11 (6 by maintainers)
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!
Added to the documentation: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/spring-2.5.x/HowTo.md