1

I am writing a test for MongoRepository custom functions class.

Here are repositories functions that I need to test:

public interface DataRepository extends MongoRepository<Data, String> {

    List<Data> getBySizeId(String sizeId);
    List<Data> findAllBy();
    List<Data> findAllBySizeId(List<String> sizeId);
}

Here is the test class:

@DataMongoTest
class DataRepositoryTest {

    @Autowired
    private DataRepository underTest;

    @Test
    void create() {
        //Given
        String id = UUID.randomUUID().toString();
        Data Data = new Data();
        Data.setId(id);
        //When
        underTest.save(Data);
        //Then
        Optional<Data> optionalData = underTest.findById(id);
        assertThat(optionalData)
                .isPresent();
    }

    @Test
    void delete() {
        //Given
        //When
        //Then
    }

    @Test
    void getBySizeId() {
        //Given
        String SizeId = UUID.randomUUID().toString();

        Data Data1 = new Data();
        Data1.setSizeId(SizeId);

        Data Data2 = new Data();
        Data2.setSizeId(SizeId);

        Data Data3 = new Data();
        Data3.setSizeId(SizeId);

        List<Data> DataList = Arrays.asList(Data1, Data2, Data3);

        //When
        underTest.saveAll(DataList);// assume that is works correct

        //Then
        List<Data> dataArray = underTest.getBySizeId(SizeId);
        assert(dataArray.stream().count() == 3);
    }
}

As you can see I use @DataMongoTest on DataRepositoryTest class.

According to documentation:

By default, tests annotated with @DataMongoTest will use an embedded in-memory MongoDB process.

While I run the test, test functions access the real database documents and not to in-memory MongoDB.

My question is any idea why it accesses the real database and not to in-memory MongoDB as it should by default according to documents?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Michael
  • 13,950
  • 57
  • 145
  • 288
  • Docs say **By default, tests annotated with @DataMongoTest will use an embedded in-memory MongoDB process (if available).**. Seems embedded db [needs configuration](https://www.baeldung.com/spring-boot-embedded-mongodb). – LMC Aug 26 '21 at 22:11

0 Answers0