Answers for "mongodb integration test with spring boot"

0

springboot mongodb test

@RunWith(SpringRunner.class)
@DataMongoTest
public class FooRepositoryTest {

    @Autowired
    FooRepository fooRepository;

    @Before
    public void setUp() throws Exception {
        fooRepository.save(new Foo());
    }

    @Test
    public void shouldBeNotEmpty() {
        assertThat(fooRepository.findAll()).isNotEmpty();
    }
}



Dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>
Posted by: Guest on June-08-2021
0

mongodb integration test with spring boot

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>



@DataMongoTest
@ExtendWith(SpringExtension.class)
public class MongoDbSpringIntegrationTest {
    @DisplayName("given object to save"
        + " when save object using MongoDB template"
        + " then object is saved")
    @Test
    public void test(@Autowired MongoTemplate mongoTemplate) {
        // given
        DBObject objectToSave = BasicDBObjectBuilder.start()
            .add("key", "value")
            .get();

        // when
        mongoTemplate.save(objectToSave, "collection");

        // then
        assertThat(mongoTemplate.findAll(DBObject.class, "collection")).extracting("key")
            .containsOnly("value");
    }
}
Posted by: Guest on June-09-2021

Code answers related to "mongodb integration test with spring boot"

Browse Popular Code Answers by Language