英文:
Can't initialize mongo db using ape-nosql-mongo @UsingDataSet
问题
我想使用Arquillian、Arquillian Cube和Mongo运行一些集成测试。所需场景如下:
- 在托管的容器中启动应用程序。在这里,我想使用Shrinkwrap仅添加我想要测试的服务(例如dao服务)。
- 在Docker容器内部启动数据库。使用一些初始数据填充数据库。
- 对数据库运行测试。
我的测试如下:
@Inject
MongoProducer producer;
@Test
@UsingDataSet(locations = "initialData.json")
public void shouldGetAllFromMongo() {
FindIterable<Document> documents = producer.getMongoClient().getDatabase("bearsdb").getCollection("bears").find();
documents.forEach((Block<? super Document>) e -> System.out.println(e));
}
initialData.json
位于 src/test/resources
目录下,种子数据的格式如下:
{
"bears": [
{
"firstName": "grizz",
"lastName": "the bear",
"age": 3
},
{
"firstName": "panpan",
"lastName": "the bear",
"age": 3
},
{
"firstName": "icebear",
"lastName": "the bear",
"age": 4
}
]
}
我的 docker-compose
文件如下:
version: '3'
services:
mongo-test-db:
image: mongo:latest
environment:
- MONGO_INITDB_DATABASE=bearsdb
- MONGO_INITDB_ROOT_USERNAME=panda
- MONGO_INITDB_ROOT_PASSWORD=pass
ports:
- 27117:27017
我不确定环境变量是否有用,但我在一个示例中看到过这个。
我的 pom.xml
包含以下依赖项:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-bom</artifactId>
<version>${version.shrinkwrap.resolvers}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- 其他依赖项 -->
</dependencies>
</dependencyManagement>
<!-- 其他依赖项 -->
请注意,我没有使用 arquillian-junit-standalone
依赖项。
附注:我正在使用 ShwripWrack 进行打包,并将 WAR 部署到托管的 Wildfly 8.2.0.Final 服务器。此外,在同一个测试类中,我还针对在 Docker 内部运行的 Postgres 进行了测试,对于这个测试,@UsingDataSet 起作用。以下是可用的 SQL 测试和 createDeploy 方法:
@Deployment
public static WebArchive createDeployment() {
JavaArchive[] javaArchives = Maven.resolver().resolve(
"org.assertj:assertj-core:3.15.0",
"org.arquillian.cube:arquillian-cube-docker:1.18.2",
"org.mongodb:mongo-java-driver:3.4.3")
.withTransitivity().as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "app.war")
// 添加类和资源等
.addAsLibraries(javaArchives)
.addAsResource("test-persistence.xml", ArchivePaths.create("META-INF/persistence.xml"))
.addAsResource("META-INF/application.properties", ArchivePaths.create("META-INF/application.properties"))
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
System.out.println(war.toString(true));
return war;
}
@Test
@org.arquillian.ape.rdbms.UsingDataSet("datasets/persons.xml")
public void shouldFindAll() {
List<Person> messages = personDao.findAll();
assertThat(messages.size()).isEqualTo(1);
}
上述测试的问题是数据库不会初始化,并且没有任何输出。
英文:
I want to run some integration test using Arquillian, Arquillian cube and Mongo. The desired scenario is:
- Start the application in a managed container. Here I want to use Shrinkwrap to add just the service I want to test (for example dao service)
- Start the database inside a docker container. Populate the db with some initial data
- Run the test against the database
My test looks like this:
@Inject
MongoProducer producer;
@Test
@UsingDataSet(locations = "initialData.json")
public void shouldGetAllFromMongo() {
FindIterable<Document> documents = producer.getMongoClient().getDatabase("bearsdb").getCollection("bears").find();
documents.forEach((Block<? super Document>) e-> System.out.println(e));
}
The initialData.json
is under src/test/resources
and format of the seeding data is as bellow:
{
"bears": [
{
"firstName": "grizz",
"lastName": "the bear",
"age": 3
},
{
"firstName": "panpan",
"lastName": "the bear",
"age": 3
},
{
"firstName": "icebear",
"lastName": "the bear",
"age": 4
}
]}
My docker-compose file looks like this:
version: '3'
services:
mongo-test-db:
image: mongo:latest
environment:
- MONGO-INITDB-DATABASE=bearsdb
- MONGO-INITDB_ROOT_USERNAME=panda
- MONGO-INITDB_ROOT_PASSWORD=pass
ports:
- 27117:27017
I don't really know if the environments help but I saw this in an example.
My pom.xml contains:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-bom</artifactId>
<version>${version.shrinkwrap.resolvers}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.15.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.arquillian</groupId>
<artifactId>arquillian-universe</artifactId>
<version>${version.arquillian_universe}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
and as dependencies
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.arquillian.cube</groupId>
<artifactId>arquillian-cube-docker</artifactId>
<version>${org.arquillian.cube.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.arquillian.universe</groupId>
<artifactId>arquillian-ape-sql-container-dbunit</artifactId>
<scope>test</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.arquillian.universe</groupId>
<artifactId>arquillian-ape-nosql-mongodb</artifactId>
<scope>test</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.3</version>
</dependency>
Please note that I don't use the arquillian-junit-standalone dependency.
Additional note is that I'm using ShwripWrack to package and I deploy the war into an managed Wildfly 8.2.0.Final server. Additionl in the same test class I've tested also against an Postgres running inside docker and for this the @UsingDataSet works ok. Bellow is the working sql test and the createDeploy method:
@Deployment
public static WebArchive createDeployment() {
JavaArchive[] javaArchives = Maven.resolver().resolve(
"org.assertj:assertj-core:3.15.0",
"org.arquillian.cube:arquillian-cube-docker:1.18.2",
"org.mongodb:mongo-java-driver:3.4.3")
.withTransitivity().as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "app.war")
.addClasses(PersonDao.class, Person.class)
.addClasses(MongoProducer.class, PropertyProducer.class, Property.class)
.addAsLibraries(javaArchives)
.addAsResource("test-persistence.xml", ArchivePaths.create("META-INF/persistence.xml"))
.addAsResource("META-INF/application.properties", ArchivePaths.create("META-INF/application.properties"))
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
System.out.println(war.toString(true));
return war;
}
@Test
@org.arquillian.ape.rdbms.UsingDataSet("datasets/persons.xml")
public void shouldFindAll() {
List<Person> messages = personDao.findAll();
assertThat(messages.size()).isEqualTo(1);
}
The issue with the above test is that the database doesn't get initialize and nothing is printed out.
答案1
得分: 0
我成功解决了我的问题。问题是我忘记添加了一个 Junit 规则,其中设置了连接到Mongo数据库的配置。
@Rule
public MongoDbRule mongoDbRule = new MongoDbRule(MongoDbConfigurationBuilder.mongoDb()
.host("localhost")
.port(27117)
.databaseName("pandadb")
.build());
完整的测试类如下:
@RunWith(Arquillian.class)
public class PersonDaoDockerIT {
@Rule
public MongoDbRule mongoDbRule = new MongoDbRule(MongoDbConfigurationBuilder.mongoDb()
.host("localhost")
.port(27117)
.databaseName("pandadb")
.build());
@Deployment
public static WebArchive createDeployment() {
JavaArchive[] javaArchives = Maven.resolver().resolve(
"org.assertj:assertj-core:3.15.0",
"org.arquillian.cube:arquillian-cube-docker:1.18.2",
"org.mongodb:mongo-java-driver:3.4.3")
.withTransitivity().as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "app.war")
.addClasses(PersonDao.class, Person.class)
.addClasses(MongoProducer.class, PropertyProducer.class, Property.class)
.addPackages(true, "com.lordofthejars.nosqlunit")
.addAsLibraries(javaArchives)
.addAsResource("test-persistence.xml", ArchivePaths.create("META-INF/persistence.xml"))
.addAsResource("META-INF/application.properties", ArchivePaths.create("META-INF/application.properties"))
.addAsResource("datasets/", ArchivePaths.create("datasets/"))
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
System.out.println(war.toString(true));
return war;
}
@Inject
PersonDao personDao;
@Inject
MongoProducer producer;
@Test
public void injectionPointShouldBeNotNull() {
assertThat(personDao).isNotNull();
}
@Test
public void mongoProducerShouldBeNotNull() {
assertThat(producer).isNotNull();
}
@Test
@org.arquillian.ape.rdbms.UsingDataSet("datasets/persons.xml")
public void shouldFindAll() {
List<Person> messages = personDao.findAll();
assertThat(messages.size()).isEqualTo(1);
}
@Test
@UsingDataSet(locations = "/datasets/initialData.json")
public void shouldGetAllFromMongo() {
FindIterable<Document> documents = producer.getMongoClient().getDatabase("pandadb").getCollection("bears").find();
documents.forEach((Block<? super Document>) System.out::println);
}
}
英文:
I managed to resolve my problem. The issue is that I forgot to add an Junit rule where the configuration to the Mongo database was set.
@Rule
public MongoDbRule mongoDbRule = new MongoDbRule(MongoDbConfigurationBuilder.mongoDb()
.host("localhost")
.port(27117)
.databaseName("pandadb")
.build());
The full test class looks like:
@RunWith(Arquillian.class)
public class PersonDaoDockerIT {
@Rule
public MongoDbRule mongoDbRule = new MongoDbRule(MongoDbConfigurationBuilder.mongoDb()
.host("localhost")
.port(27117)
.databaseName("pandadb")
.build());
@Deployment
public static WebArchive createDeployment() {
JavaArchive[] javaArchives = Maven.resolver().resolve(
"org.assertj:assertj-core:3.15.0",
"org.arquillian.cube:arquillian-cube-docker:1.18.2",
"org.mongodb:mongo-java-driver:3.4.3")
.withTransitivity().as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "app.war")
.addClasses(PersonDao.class, Person.class)
.addClasses(MongoProducer.class, PropertyProducer.class, Property.class)
.addPackages(true, "com.lordofthejars.nosqlunit")
.addAsLibraries(javaArchives)
.addAsResource("test-persistence.xml", ArchivePaths.create("META-INF/persistence.xml"))
.addAsResource("META-INF/application.properties", ArchivePaths.create("META-INF/application.properties"))
.addAsResource("datasets/", ArchivePaths.create("datasets/"))
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
System.out.println(war.toString(true));
return war;
}
@Inject
PersonDao personDao;
@Inject
MongoProducer producer;
@Test
public void injectionPointShouldBeNotNull() {
assertThat(personDao).isNotNull();
}
@Test
public void mongoProducerShouldBeNotNull() {
assertThat(producer).isNotNull();
}
@Test
@org.arquillian.ape.rdbms.UsingDataSet("datasets/persons.xml")
public void shouldFindAll() {
List<Person> messages = personDao.findAll();
assertThat(messages.size()).isEqualTo(1);
}
@Test
@UsingDataSet(locations = "/datasets/initialData.json")
public void shouldGetAllFromMongo() {
FindIterable<Document> documents = producer.getMongoClient().getDatabase("pandadb").getCollection("bears").find();
documents.forEach((Block<? super Document>) System.out::println);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论