From 8cd148b5b02948801c21adf5d50ed5f2c15ce7bd Mon Sep 17 00:00:00 2001 From: Harry9656 Date: Wed, 20 Mar 2024 14:15:54 +0100 Subject: [PATCH] JAVA-31697: Fix failing integration test in spring-boot-persistence-mongodb (#16177) --- .../src/main/resources/application.properties | 3 ++- ...goDbAutoGeneratedFieldIntegrationTest.java | 25 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties index 65e65cc96c..2b37be6e57 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties @@ -10,4 +10,5 @@ spring.thymeleaf.cache=false spring.servlet.multipart.max-file-size=256MB spring.servlet.multipart.max-request-size=256MB -spring.servlet.multipart.enabled=true \ No newline at end of file +spring.servlet.multipart.enabled=true +spring.data.mongodb.uri=mongodb://localhost \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java index e20a229f36..9be3c7e171 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/MongoDbAutoGeneratedFieldIntegrationTest.java @@ -2,30 +2,31 @@ package com.baeldung.mongodb; import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import com.baeldung.mongodb.daos.UserRepository; import com.baeldung.mongodb.models.User; - -@RunWith(SpringRunner.class) -@SpringBootTest +@ExtendWith(SpringExtension.class) @DirtiesContext -public class MongoDbAutoGeneratedFieldIntegrationTest { +@DataMongoTest +class MongoDbAutoGeneratedFieldIntegrationTest { @Autowired UserRepository userRepository; @Test - public void contextLoads() {} + void contextLoads() { + // Verifies the context is loaded correctly + } @Test - public void givenUserObject_whenSave_thenCreateNewUser() { + void givenUserObject_whenSave_thenCreateNewUser() { User user = new User(); user.setFirstName("John"); @@ -33,8 +34,8 @@ public class MongoDbAutoGeneratedFieldIntegrationTest { user.setEmail("john.doe@example.com"); userRepository.save(user); - assertThat(userRepository.findAll().size()).isGreaterThan(0); + assertThat(userRepository.findAll() + .size()).isPositive(); } - }