mercredi 10 octobre 2018

Embedded mongodb thow MongoSocketReadException in tests

I configured embedded mongodb for test. If all of them executed the MongoSocketReadException: Exception receiving message Caused by: java.net.SocketException: Connection reset appears in different test. However when the each of them executed one by one there is no any issue.

The configuration is made in following way

   @DependsOn("embeddedMongoServer") // name has been taken from org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration.embeddedMongoServer
@Bean                                    // client should start after server start
public MongoClient getMongoClient(IMongodConfig: mongodConfig){
    return new MongoClient(mongodConfig.net().getSserverAddress.getHostName, mongodConfig.net().getPort)
}

and the main class has the following annotation: @SpringBootApplication(exclude = [MongoAutoConfiguration::class, MongoDataAutoConfiguration::class, EmbeddedMongoAutoConfiguration::class])

in the pom I have the following (spring data is not used):

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.6.4</version>
    </dependency>

The tests use mongodb in the following way:

@ExtendWith(SpringExtension::class)
@SpringBootTest

class SomeTest{

@Resource
private MongoClient mongoClient;
private MongoDatabase db;

@BeforeEach
public void prepareDb() {
    db = mongoClient.getDatabase("dbName")
    mongoCollection = db.getCollection("collection")
}

class BasicTestsWithSampleData {
 @BeforeEach
    public populateDb(){
        mongoCollection.insertOne(Document.parse(sampleRecording))
    }

    @AfterEach
    public clearDb(){
        mongoCollection.drop()
    }
}
}

Aucun commentaire:

Enregistrer un commentaire