How to use these @DataMongoTest and @SpringBootTest together in integration test
Asked Answered
B

3

21

I am trying to write integration test case for one of my rest application which uses mongodb internally to persist the data

@DataMongoTest 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MainControllerTest {
@LocalServerPort
    private int port = 8080;
/* some test cases*/ 
}

but I am getting below error

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.sample.core.controller.MainControllerTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]

looks like these two are mutually exclusive, so how to do the integration testing .

Backblocks answered 2/4, 2019 at 11:51 Comment(3)
See this question, comment and answer: #45861399Dethrone
Possible duplicate of Springboot: How can I perform an integration test with real dependencies?Dethrone
this solved my question: #48040144Backblocks
R
17

Use @AutoConfigureDataMongo with @SpringBootTest and this will resolve this ambiguity issue. @SpringBootTest and @DataMongoTest cannot be used together.

Runofthemine answered 24/9, 2020 at 7:27 Comment(0)
H
0

Answering to a very old post hoping it may help others. @AutoConfigureDataMongo will connect to real database. In order to still use the embedded mongo, one can initiate the embedded mongoDb manually.

@SpringBootTest(classes = SubscriptionEventApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SubscriptionEventApiIntegrationTest {


@BeforeAll
    static void setup() throws Exception {
        startEmbeddedMongoDbManually();
    }
    
    private static void startEmbeddedMongoDbManually() throws IOException {
        final String connectionString = "mongodb://%s:%d";
        final String ip = "localhost";
        final int port = 27017;
    
        ImmutableMongodConfig mongodConfig = MongodConfig
                .builder()
                .version(Version.V3_5_5)
                .net(new Net(ip, port, Network.localhostIsIPv6()))
                .build();
    
        MongodStarter starter = MongodStarter.getDefaultInstance();
        mongodExecutable = starter.prepare(mongodConfig);
        mongodExecutable.start();
        mongoTemplate = new MongoTemplate(MongoClients.create(String.format(connectionString, ip, port)), "test");
    }
    
    @AfterAll
    static void clean() {
        mongodExecutable.stop();
    }

    @Test
    public void test() {
    .....
    }
 }
Heron answered 26/9, 2022 at 10:9 Comment(0)
R
0

Purushothaman suggested starting embedded MongoDB server manually. I am suggesting to start it automatically using @DataMongoTest, but creating WebTestClient manually instead.

Kotlin code below, translates to Java trivially:

@DataMongoTest
// @ContextConfiguration may not be needed for your case.
@ContextConfiguration(
    classes = [
        Application::class,
        MainController::class,
        // Add more needed classes for your tests here.
        // ...
    ]
)
@TestPropertySource(properties = ["spring.mongodb.embedded.version=4.0.12"])
class MainControllerTest(
    @Autowired
    private val mainController: MainController,
    // Add more beans needed for your tests here.
    // ...
) {
    // Creating a WebTestClient is easy and
    // can be done in different ways.
    // Here is one of the possible ways.
    private val webTestClient: WebTestClient =
        WebTestClient.bindToController(mainController).build()

    @Test
    fun someTest() {
        // ...
    }
}
Riella answered 20/2, 2023 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.