I have a service that find a object in the repository and return one attribute of the object. I want try test service mocking access to repository, but when i call the service method return null.
My service:
@Service
public class MyService{
public Object getModuleObj(String id, String module) {
Object repObj = Repository.findOne(id,module);
return repObj.getModule();
}
}
My test class:
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(MyController.class)
@ContextConfiguration(classes = AnnotationConfigContextLoader.class)
public class ApiTest {
@MockBean
public MyRepository repository;
@MockBean
private MyService service;
@Test
public void testGetObjectOfRepo() throws Exception {
Object obj = new Object();
Module mod = new Module();
mod.setId("6");
mod.setModule("test");
obj.add(mod);
/** mock repository response **/
Mockito.when(repository.findOne("6","test")).thenReturn(obj);
/** invoke service method**/
String module= "test";
/** serviceResponse return null **/
Module serviceResponse = this.service.getModuleObj("6","test");
Assert.assertEquals(mod, serviceResponse);
}
}
Comparison between objects fail because serviceResponse is null. It's correct as i'm mocking the repository?
Aucun commentaire:
Enregistrer un commentaire