ben test yönteminden çağrılacak bir hizmet sınıfına sahip bir örnek için:
class A {
public int test(final String s) {
return 0;
}
public int test(final int i) {
return 0;
}
}
Ve test yönteminde
public interface AService{
public ReturnObject addNewItem(String param1, String param2);
public ReturnObject addNewItem(String param1, String param2, boolean isOk);
}
MainServiceImpl sınıfında bir yöntemi var olacak
@Service("mainService")
public class MainServiceImpl implements MainService {
@Autowired
private AService aService;
public ReturnObject saveItem(String itemName, String itemCode){
return aService.addNewItem(itemName, itemCode);
}
}
: aşırı yüklenmiş bir yöntemi, aşağıdaki gibi çağrı
yüzden saveItem
Eğer cevap nesne karşılığında istediğini döndürmez cevap daha sonra bir taklidini oluşturmak için normal şekilde kullanılan olsaydı zaten addNewItem
olarak aşırı yönteme çağıran yöntem için birim test yazmak zorunda olduğunda.
@RunWith(PowerMockRunner.class)
@PrepareForTest({ })
public class ItemTest{
@Test
public void testSaveItem() throws Exception {
//create a real object of MainServiceImpl
MainServiceImpl mainService = new MainServiceImpl();
//create a normal way for a mocking object
AService aService = Mockito.mock(AService.class);
// Add mock object to MainServiceImpl instance
ReflectionTestUtils.setField(mainService, "aService", aService);
//Mock when aService call to addNewItem() method
PowerMockito.when(aService , "addNewItem", Mockito.anyString(), Mockito.anyString()).then(new Answer<ReturnObject>() {
@Override
public ReturnObject answer(InvocationOnMock invocation) throws Throwable {
return new ReturnObject("saveOK");
}
});
ReturnObject returnObj = mainService.saveItem("Book", "Code123");
Assert.assertNotNull(returnObj);
}
}
deneyin sonra başarı aşağıda testSaveItem ile yukarıda testSaveItem yerine:
@Test
public void testSaveItem() throws Exception {
//create a real object of MainServiceImpl
MainServiceImpl mainService = new MainServiceImpl();
//create a special way for a mocking object by add
//the answer at create the mock object
final Answer<ReturnObject> answer = new Answer<ReturnObject>() {
@Override
public ReturnObjectanswer(final InvocationOnMock invocation) throws Throwable {
return new ReturnObject("saveOK");
}
};
AService aService = Mockito.mock(AService.class, answer);
// Add mock object to MainServiceImpl instance
ReflectionTestUtils.setField(mainService, "aService", aService);
//Mock when aService call to addNewItem() method
PowerMockito.when(aService , "addNewItem", Mockito.anyString(), Mockito.anyString()).then(new Answer<ReturnObject>() {
@Override
public ReturnObject answer(InvocationOnMock invocation) throws Throwable {
return new ReturnObject("saveOK");
}
});
ReturnObject returnObj = mainService.saveItem("Book", "Code123");
Assert.assertNotNull(returnObj);
}
Benzer http://stackoverflow.com/questions/5594645/verify-whether-one-of-three-methods için -is-çağrılan-ile-mockito – BretC
Yani ben bir mockbuilder yönteminde her iki mocks alamayan anladın mı? – Chry007
Ben – BretC