question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to run consecutive test suites due to error

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
yyc1217commented, Mar 17, 2014

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")
0reactions
michaelomcommented, Jan 10, 2015

Here’s my implementation for reference:

public class EmbeddedMongoClient {

    private static final MongodStarter starter = MongodStarter.getInstance(newRuntimeConfig());
    public static final int DEFAULT_MONGO_PORT = 27017;

    private final int port;

    private MongodExecutable mongodExe;
    private MongodProcess mongod;

    public EmbeddedMongoClient(int port) {
        this.port = port;
    }

    public EmbeddedMongoClient() {
        this(DEFAULT_MONGO_PORT);
    }

    public void start() throws Throwable {
        mongodExe = starter.prepare(new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(port, Network.localhostIsIPv6()))
                .build());
        mongod = mongodExe.start();

    }

    public void stop() {
        mongodExe.stop();
        mongod.stop();
    }

    private static IRuntimeConfig newRuntimeConfig() {
        Command command = Command.MongoD;
        return new RuntimeConfigBuilder()
                .defaults(command)
                .processOutput(ProcessOutput.getDefaultInstanceSilent())
                .artifactStore(new ArtifactStoreBuilder()
                        .defaults(command)
                        .executableNaming((prefix, postfix) -> "embeddedmongo")
                        .useCache(false))
                .build();
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Xcode 12 issues running multiple t… | Apple Developer Forums
Xcode 12 issues running multiple test suites with 'Underlying Error: Invalid device state' [only when running via SSH].
Read more >
Consecutive test cases fails with Error: Not supported #910
Current Behavior When executing several tests in the same file each consecutive test fails with the following error: Error: Uncaught [Error: ...
Read more >
Unable to launch multiple test cases in test Suite ...
Hi, It shows me a problem (An internal error occurred during: “Launching the test suite…”. Java heap space) when I run a test...
Read more >
'Unable to load a Suite class' while running ScalaTest in IntelliJ
I'm basically "right-clicking" on the test and selecting "Run". What I get is the following error: "C:\Program Files\Java\jdk1.7.0_25\bin\java" ...
Read more >
Can't use a Test Case twice in a Test Plan | Support
It appears that it is impossible to add a given Test Case to a Test Plan more than once. (I get an error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found