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

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.jsonsrc/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>
        <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>

并作为依赖项

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

请注意,我没有使用 arquillian-junit-standalone 依赖项。

另外请注意,我正在使用 ShwripWrack 打包并将 war 部署到托管的 Wildfly 8.2.0.Final 服务器中。 Additionl 在同一测试中 class 我还在 docker 中针对 Postgres 运行ning 进行了测试,为此 @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")

            .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);
}

上述测试的问题是数据库没有初始化,也没有打印任何内容。

我设法解决了我的问题。问题是我忘记添加一个 Junit 规则,其中设置了 Mongo 数据库的配置。

@Rule
public MongoDbRule mongoDbRule = new MongoDbRule(MongoDbConfigurationBuilder.mongoDb()
        .host("localhost")
        .port(27117)
        .databaseName("pandadb")
        .build());

完整测试 class 看起来像:

@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);
}
}