Skip to content

Instantly share code, notes, and snippets.

@djegauth
Created February 26, 2014 22:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djegauth/9240595 to your computer and use it in GitHub Desktop.
Save djegauth/9240595 to your computer and use it in GitHub Desktop.
MongoDB / Arquillian / NoSqlUnit
public class DeploymentTest {
@Deployment
public static Archive<?> createDeployment() {
File[] libs = Maven
.resolver()
.loadPomFromFile("pom.xml")
.resolve("org.slf4j:slf4j-api:1.7.5",
"org.mongodb:mongo-java-driver:jar:2.11.2",
"com.lordofthejars:nosqlunit-mongodb:0.7.9",
"org.assertj:assertj-core:1.5.0",
"de.flapdoodle.embed:de.flapdoodle.embed.mongo:jar:1.41",
"org.jongo:jongo:1.0",
"com.fasterxml.jackson.datatype:jackson-datatype-joda:2.2.1",
"com.google.guava:guava:16.0",
"com.google.code.gson:gson:2.1",)
.withTransitivity().asFile();
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackages(true, "com.test")
.addAsResource("com/test/service/initialUser.json", "com/test/service/initialUser.json")
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
.addAsLibraries(libs);
}
}
{
"user": [
{ "_id" : { "$oid" : "5169aa28e4b0266f45f5a6e7" }, "name" : "Jérémie Gauthier" },
{ "_id" : { "$oid" : "5175aff8e4b0ee583c98cde6" }, "name" : "djegauth" },
{ "_id" : { "$oid" : "518043c9e4b008ed8f6e909f" }, "name" : "qsdf", "password" : "782dd27ea8e3b4f4095ffa38eeb4d20b59069077" },
{ "_id" : { "$oid" : "51883928e4b0a36d741b41f5" }, "name" : "azerty" }
]
}
<plugin>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin</artifactId>
<version>0.1.10</version>
<executions>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<port>27017</port>
<!-- optional, default 27017 -->
<!--<randomPort>true</randomPort> &lt;!&ndash; optional, default is false, if true allocates a random port and overrides embedmongo.port &ndash;&gt;-->
<version>2.2.3</version>
<!-- optional, default 2.2.1 -->
<databaseDirectory>${project.build.directory}\mongo-temp\mongotest</databaseDirectory>
<!-- optional, default is a new dir in java.io.tmpdir -->
<logging>none</logging>
<!-- optional (file|console|none), default console -->
<!--<logFile>${project.build.directory}/myfile.log</logFile> &lt;!&ndash; optional, can be used when logging=file, default is ./embedmongo.log &ndash;&gt;-->
<!--<logFileEncoding>utf-8</logFileEncoding> &lt;!&ndash; optional, can be used when logging=file, default is utf-8 &ndash;&gt;-->
<!--<proxyHost>myproxy.company.com</proxyHost> &lt;!&ndash; optional, default is none &ndash;&gt;-->
<!--<proxyPort>8080</proxyPort> &lt;!&ndash; optional, default 80 &ndash;&gt;-->
<!--<proxyUser>joebloggs</proxyUser> &lt;!&ndash; optional, default is none &ndash;&gt;-->
<!--<proxyPassword>pa55w0rd</proxyPassword> &lt;!&ndash; optional, default is none &ndash;&gt;-->
<!--<bindIp>127.0.0.1</bindIp> &lt;!&ndash; optional, default is to listen on all interfaces &ndash;&gt;-->
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
@RunWith(Arquillian.class)
@UsingDataSet(locations = {"/com/test/service/initialUser.json"}, loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public class UserServiceIT extends DeploymentTest {
@ClassRule
public static ManagedMongoDb managedMongoDb = newManagedMongoDbRule()
.mongodPath("mongotest")
.build();
@Rule
public MongoDbRule remoteMongoDbRule = new MongoDbRule(mongoDb()
.databaseName("dbtest")
.host("localhost")
.port(27017)
.build());
@ConnectionManager
private Mongo mongo;
@Inject
@MongoDB
private UserService userService;
@Before
public void before() {
Jongo jongo = new Jongo(this.mongo.getDB("dbtest"), new JacksonMapper.Builder().build());
userService.setJongo(jongo);
}
@Test
public void testGetUsers() {
List<User> users = userService.getUsers();
assertThat(users).isNotNull().hasSize(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment