de.flapdoodle.embed.mongo: Unable to run consecutive test suites due to error

We have two Junit tests that each declare @BeforeClass and @AfterClass methods to setup and tear down the MongodExecutable instance, for example:


@BeforeClass
public static void setupClass() throws Exception {
    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(mongoPort, Network.localhostIsIPv6()))
            .build();
    MongodStarter runtime = MongodStarter.getDefaultInstance();
    mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();
    mongo = new MongoClient("localhost", mongoPort);
    db = mongo.getDB("mydb");
}

@AfterClass
public static void tearDownClass() {
    if (mongodExecutable != null)
        mongodExecutable.stop();
}

Running each of these tests separately works fine, but running in the same suite the second test to run fails and reports:

WARNING: emptying DBPortPool to localhost/127.0.0.1:27017 b/c of error

Followed by a broken pipe error from the code attempting to access the DB.

In the @Before method for each test we are loading test data by executing db.doEval(...); and in the @After we are doing the same with a dropDatabase() call to clean up.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Hi, I’ve got same issue, and my tests are passed after adding @DirtiesContext to each test method:

import static org.junit.Assert.*;

import org.jongo.MongoCollection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DaoTestConfig.class, loader = AnnotationConfigContextLoader.class)
public class AbstractDaoTest {

    @Autowired
    private TestObjectDao testObjectDao;

    private MongoCollection col;
    private MongodExecutable _mongodExe;
    private MongodProcess _mongod;

    private static final TestObject TEST_OBJ = new TestObject("ABC", 123);
    private static final String TEST_STR = "{'string': 'ABC', 'integer' : 123}";

    @Before
    public void setUp() throws Exception {

        col = this.testObjectDao.getCollection();

        MongodStarter runtime = MongodStarter.getDefaultInstance();
        _mongodExe = runtime.prepare(new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(12345, Network.localhostIsIPv6())).build());
        _mongod = _mongodExe.start();
    }

    @After
    public void tearDown() throws Exception {
        if (_mongod != null) {
            _mongod.stop();
        }

        if (_mongodExe != null) {
            _mongodExe.stop();
        }
    }

    @Test
    @DirtiesContext
    public void canSavePojo() {
        this.testObjectDao.save(TEST_OBJ);
        assertEquals(1, col.count(TEST_STR));
    }

    @Test
    @DirtiesContext
    public void canSave() {
        this.testObjectDao.save(TEST_STR);
        assertEquals(1, col.count(TEST_STR));
    }

}

and some test dependencies in build.gradle:

     //mongodb java driver
    compile("org.jongo:jongo:1.0")
    compile("org.mongodb:mongo-java-driver:2.11.4")

    testCompile("junit:junit:4.11")
    testCompile("org.springframework:spring-test:3.2.4.RELEASE")
    testCompile("de.flapdoodle.embed:de.flapdoodle.embed.mongo:1.42")