无法使用 @UsingDataSet 初始化 ape-nosql-mongo 的 MongoDB 数据库。

huangapple go评论63阅读模式
英文:

Can't initialize mongo db using ape-nosql-mongo @UsingDataSet

问题

我想使用Arquillian、Arquillian Cube和Mongo运行一些集成测试。所需场景如下:

  1. 在托管的容器中启动应用程序。在这里,我想使用Shrinkwrap仅添加我想要测试的服务(例如dao服务)。
  2. 在Docker容器内部启动数据库。使用一些初始数据填充数据库。
  3. 对数据库运行测试。

我的测试如下:

@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:

  1. 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)
  2. Start the database inside a docker container. Populate the db with some initial data
  3. Run the test against the database

My test looks like this:

@Inject
MongoProducer producer;

@Test
@UsingDataSet(locations = &quot;initialData.json&quot;)
public void shouldGetAllFromMongo() {

    FindIterable&lt;Document&gt; documents = producer.getMongoClient().getDatabase(&quot;bearsdb&quot;).getCollection(&quot;bears&quot;).find();
    documents.forEach((Block&lt;? super Document&gt;) e-&gt; System.out.println(e));
}

The initialData.json is under src/test/resources and format of the seeding data is as bellow:

{
  &quot;bears&quot;: [
  {
    &quot;firstName&quot;: &quot;grizz&quot;,
    &quot;lastName&quot;: &quot;the bear&quot;,
    &quot;age&quot;: 3
  },
  {
    &quot;firstName&quot;: &quot;panpan&quot;,
    &quot;lastName&quot;: &quot;the bear&quot;,
    &quot;age&quot;: 3
  },
  {
    &quot;firstName&quot;: &quot;icebear&quot;,
    &quot;lastName&quot;: &quot;the bear&quot;,
    &quot;age&quot;: 4
  }
]}

My docker-compose file looks like this:

version: &#39;3&#39;
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:

&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.jboss.shrinkwrap.resolver&lt;/groupId&gt;
            &lt;artifactId&gt;shrinkwrap-resolver-bom&lt;/artifactId&gt;
            &lt;version&gt;${version.shrinkwrap.resolvers}&lt;/version&gt;
            &lt;scope&gt;import&lt;/scope&gt;
            &lt;type&gt;pom&lt;/type&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.jboss.arquillian&lt;/groupId&gt;
            &lt;artifactId&gt;arquillian-bom&lt;/artifactId&gt;
            &lt;version&gt;1.1.15.Final&lt;/version&gt;
            &lt;scope&gt;import&lt;/scope&gt;
            &lt;type&gt;pom&lt;/type&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.arquillian&lt;/groupId&gt;
            &lt;artifactId&gt;arquillian-universe&lt;/artifactId&gt;
            &lt;version&gt;${version.arquillian_universe}&lt;/version&gt;
            &lt;scope&gt;import&lt;/scope&gt;
            &lt;type&gt;pom&lt;/type&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;

and as dependencies

    &lt;dependency&gt;
        &lt;groupId&gt;org.jboss.arquillian.junit&lt;/groupId&gt;
        &lt;artifactId&gt;arquillian-junit-container&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.arquillian.cube&lt;/groupId&gt;
        &lt;artifactId&gt;arquillian-cube-docker&lt;/artifactId&gt;
        &lt;version&gt;${org.arquillian.cube.version}&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.arquillian.universe&lt;/groupId&gt;
        &lt;artifactId&gt;arquillian-ape-sql-container-dbunit&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
        &lt;type&gt;pom&lt;/type&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.arquillian.universe&lt;/groupId&gt;
        &lt;artifactId&gt;arquillian-ape-nosql-mongodb&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
        &lt;type&gt;pom&lt;/type&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
        &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
        &lt;version&gt;3.4.3&lt;/version&gt;
    &lt;/dependency&gt;

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(
            &quot;org.assertj:assertj-core:3.15.0&quot;,
            &quot;org.arquillian.cube:arquillian-cube-docker:1.18.2&quot;,
            &quot;org.mongodb:mongo-java-driver:3.4.3&quot;)
            .withTransitivity().as(JavaArchive.class);

    WebArchive war = ShrinkWrap.create(WebArchive.class, &quot;app.war&quot;)

            .addClasses(PersonDao.class, Person.class)
            .addClasses(MongoProducer.class, PropertyProducer.class, Property.class)
            .addAsLibraries(javaArchives)
            .addAsResource(&quot;test-persistence.xml&quot;, ArchivePaths.create(&quot;META-INF/persistence.xml&quot;))
            .addAsResource(&quot;META-INF/application.properties&quot;, ArchivePaths.create(&quot;META-INF/application.properties&quot;))
            .addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create(&quot;beans.xml&quot;));
    System.out.println(war.toString(true));
    return war;
}

@Test
@org.arquillian.ape.rdbms.UsingDataSet(&quot;datasets/persons.xml&quot;)
public void shouldFindAll() {
    List&lt;Person&gt; 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(&quot;localhost&quot;)
.port(27117)
.databaseName(&quot;pandadb&quot;)
.build());

The full test class looks like:

@RunWith(Arquillian.class)
public class PersonDaoDockerIT {
@Rule
public MongoDbRule mongoDbRule = new MongoDbRule(MongoDbConfigurationBuilder.mongoDb()
.host(&quot;localhost&quot;)
.port(27117)
.databaseName(&quot;pandadb&quot;)
.build());
@Deployment
public static WebArchive createDeployment() {
JavaArchive[] javaArchives = Maven.resolver().resolve(
&quot;org.assertj:assertj-core:3.15.0&quot;,
&quot;org.arquillian.cube:arquillian-cube-docker:1.18.2&quot;,
&quot;org.mongodb:mongo-java-driver:3.4.3&quot;)
.withTransitivity().as(JavaArchive.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, &quot;app.war&quot;)
.addClasses(PersonDao.class, Person.class)
.addClasses(MongoProducer.class, PropertyProducer.class, Property.class)
.addPackages(true, &quot;com.lordofthejars.nosqlunit&quot;)
.addAsLibraries(javaArchives)
.addAsResource(&quot;test-persistence.xml&quot;, ArchivePaths.create(&quot;META-INF/persistence.xml&quot;))
.addAsResource(&quot;META-INF/application.properties&quot;, ArchivePaths.create(&quot;META-INF/application.properties&quot;))
.addAsResource(&quot;datasets/&quot;, ArchivePaths.create(&quot;datasets/&quot;))
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create(&quot;beans.xml&quot;));
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(&quot;datasets/persons.xml&quot;)
public void shouldFindAll() {
List&lt;Person&gt; messages = personDao.findAll();
assertThat(messages.size()).isEqualTo(1);
}
@Test
@UsingDataSet(locations = &quot;/datasets/initialData.json&quot;)
public void shouldGetAllFromMongo() {
FindIterable&lt;Document&gt; documents = producer.getMongoClient().getDatabase(&quot;pandadb&quot;).getCollection(&quot;bears&quot;).find();
documents.forEach((Block&lt;? super Document&gt;) System.out::println);
}
}

huangapple
  • 本文由 发表于 2020年8月20日 18:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定