I'm writing service tests for my Spring Boot Web application that acts as an interface to MongoDB. Ideally, my service test will test each component of my Spring application before finally hitting a Mocked MongoTemplate. The following code uses a MockMvc to hit my web endpoints.
@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
@AutoConfigureDataMongo
public class MyControllerServiceTest {
@Autowired
private MockMvc mvc;
@Autowired
private MongoTemplate mongoTemplate
@SpyBean
private MyMongoRepository myMongoRepository;
@Test
public void createTest() {
MyObject create = new MyObject()
given(this.myMongoRepository.insert(create));
this.mvc.perform(post("localhost:8080/myService")...)...;
}
}
MyController contains an @Autowired MyMongoRepository, which in turn implements MongoRepository and necessitates a mongoTemplate bean. This code executes properly only if a running MongoDB instance can be found (this example is more of an integration test between my service and MongoDB).
How can I mock out MongoTemplate while still using my MockMvc?