The Spring TestContext Framework provides annotation-driven unit and integration testing support. Fongo is an in-memory java implementation of MongoDB.
embedded-mongo-spring provides a way to use fongo with Spring TestContext Framework.
The magic is in FongoTestExecutionListener class (default listener) which implements TestExecutionListener. This listener find @EmbeddedMongo annotation:
@EmbeddedMongo: annotation to start an embedded mongodb server
- dbName: database name; "myDbName" by default
In your pom.xml, you have to add embedded-mongo-spring maven dependency:
<dependency>
<groupId>com.github.mwarc</groupId>
<artifactId>embedded-mongo-spring</artifactId>
<version>0.1.0</version>
</dependency>
or when you use gradle add to build.gradle:
dependencies {
testCompile 'com.github.mwarc:embedded-mongo-spring:0.1.0'
}
The following snippet use basic Spring configuration and @EmbeddedMongo. Default listener FongoTestExecutionListener find @EmbeddedMongo annotation and try to start an embedded mongodb (database myDbName).
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@TestExecutionListeners({
FongoTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class})
@EmbeddedMongo(dbName = "myDbName")
public class EmbeddedMongoTest {
@Test
public void shouldSaveUserAndRetrieveItFromMongo() {
...
}
}
The following snippet use basic Spring configuration and @EmbeddedMongo. Default listener FongoTestExecutionListener find @EmbeddedMongo annotation and try to start an embedded mongodb with default configuration.
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@EmbeddedMongo
class EmbeddedMongoSpec extends Specification {
def "should save user and retrieve it from mongo"() {
...
}
}
- Clone the repository
- Run
./gradlew clean build
(on Linux/Mac) orgradlew.bat clean build
(on Windows)
Apache Licence v2.0 (see LICENCE.txt)