无法使用ape nosql mongo@usingdataset初始化mongo db

dgjrabp2  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(377)

我想使用arquillian、arquillian cube和mongo运行一些集成测试。理想的情况是:
在托管容器中启动应用程序。这里我想使用包覆面处理来添加我想要测试的服务(例如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

我不知道环境是否有帮助,但我在一个例子中看到了这一点。
my 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独立依赖关系。
另外需要注意的是,我使用shwripwrack来打包war,并将其部署到托管的wildfly 8.2.0.final服务器中。在同一个测试类中,我还针对docker内部运行的postgres测试了additionl,@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);
}

上面的测试的问题是数据库没有初始化,没有输出任何内容。

l5tcr1uw

l5tcr1uw1#

我设法解决了我的问题。问题是我忘了在mongo数据库的配置被设置的地方添加junit规则。

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

相关问题