0

I would like to set up a test (unit or integration test) for a mongo query. I would like to test the following function:

 public ArrayList<Document> search(){ 
         Document textSearch = new Document("$text",new 
         Document("$search",text));
         return randomCollection.find(textSearch).into(new ArrayList<Document>());
    }

Im using MongoTemplate to get the mongo collection randomCollection

user1796624
  • 3,665
  • 7
  • 38
  • 65

1 Answers1

0

@SpringBootTest can be used to bootstrap all your Spring configurations. If you will write a test (which you should always do, your test will look something like this):

@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeArbitraryTests {

    @Autowired
    private ArbitraryResource someResource;

    @Test
    public void someTest() {
        someResource.search(...);
        // assertions
    }
}

If you would want to add an Embedded Mongodb for your testing purposes, then you might want to add some additional dependencies to your project:

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

Hope this helps!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
  • thanks for your answer i still have something unclear: Im using a @Configuration annotated class to get a mongo bean in the service , how would I switch the real mongo in the service with the embedded mongo so i would be able to test the service, and is this what you have suggested an integration test? – user1796624 May 01 '18 at 23:44