springboot mongodb test code example

Example: 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>

Tags:

Misc Example