javajava-ee-7jboss-arquillian

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


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 = "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.


Solution

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