mockito 验证方法内部方法调用
mockito verify method call inside method
对于单元测试,我正在尝试验证是否有
一种使用 mockito verify 验证方法内部方法调用的方法?
例如:
public delete(param) {
VideoService.deleteVideo(param); << i want to verify the call of this method
return etc..
}
我可以检查是否调用了 delete :
verify(mock,times(1)).delete(param);
是否还有一种方法可以检查内部方法,例如:
验证(模拟,时间(1))。删除(参数)。VideoService.deleteVideo(参数);
假设您有 class
class MyVideoService {
final VideoService videoService;
public MyVideoService(VideoService videoService) {
this.videoService = videoService;
}
public void delete(param) {
videoService.deleteVideo(param);
}
}
然后你用
模拟VideoService
VideoService videoService = mock(VideoService.class);
并使用这个模拟实例创建 MyVideoService,调用方法,验证:
MyVideService myVideoService = new MyVideoService(videoService);
myVideoService.delete (param);
verify(videoService, times(1)).deleteVideo(param);
你可以使用间谍。
public class MyVideoService {
private VideoService videoService;
public MyVideoService(VideoService videoService) {
this.videoService = videoService;
}
public void delete(String param) {
videoService.deleteVideo(param);
}
}
public class VideoService {
public void deleteVideo(String param) {
}
}
如果您现在要测试使用 MyVideoService
的对象。例如
public class ObjectThatUsesMyVideoService {
private MyVideoService myVideoService;
ObjectThatUsesMyVideoService(MyVideoService myVideoService) {
this.myVideoService = myVideoService;
}
public void deleteVideo(String param) {
myVideoService.delete(param);
}
}
你可以这样写一个测试
public class MyVideoServiceTest {
@Test
public void delete(){
// VideoService is just a mock
VideoService videoServiceMock = Mockito.mock(VideoService.class);
// Creating the real MyVideoService
MyVideoService myVideoService = new MyVideoService(videoServiceMock);
// Creating a spy proxy
MyVideoService myVideoServiceSpy = Mockito.spy(myVideoService);
ObjectThatUsesMyVideoService underTest = new ObjectThatUsesMyVideoService(myVideoServiceSpy);
underTest .deleteVideo("SomeValue");
// Verify that myVideoService was invoked
Mockito.verify(myVideoServiceSpy, Mockito.times(1)).delete("SomeValue");
// Verify that myVideoService invoked the VideoService
Mockito.verify(videoServiceMock, Mockito.times(1)).deleteVideo("SomeValue");
}
}
对于单元测试,我正在尝试验证是否有 一种使用 mockito verify 验证方法内部方法调用的方法?
例如:
public delete(param) {
VideoService.deleteVideo(param); << i want to verify the call of this method
return etc..
}
我可以检查是否调用了 delete :
verify(mock,times(1)).delete(param);
是否还有一种方法可以检查内部方法,例如: 验证(模拟,时间(1))。删除(参数)。VideoService.deleteVideo(参数);
假设您有 class
class MyVideoService {
final VideoService videoService;
public MyVideoService(VideoService videoService) {
this.videoService = videoService;
}
public void delete(param) {
videoService.deleteVideo(param);
}
}
然后你用
模拟VideoServiceVideoService videoService = mock(VideoService.class);
并使用这个模拟实例创建 MyVideoService,调用方法,验证:
MyVideService myVideoService = new MyVideoService(videoService);
myVideoService.delete (param);
verify(videoService, times(1)).deleteVideo(param);
你可以使用间谍。
public class MyVideoService {
private VideoService videoService;
public MyVideoService(VideoService videoService) {
this.videoService = videoService;
}
public void delete(String param) {
videoService.deleteVideo(param);
}
}
public class VideoService {
public void deleteVideo(String param) {
}
}
如果您现在要测试使用 MyVideoService
的对象。例如
public class ObjectThatUsesMyVideoService {
private MyVideoService myVideoService;
ObjectThatUsesMyVideoService(MyVideoService myVideoService) {
this.myVideoService = myVideoService;
}
public void deleteVideo(String param) {
myVideoService.delete(param);
}
}
你可以这样写一个测试
public class MyVideoServiceTest {
@Test
public void delete(){
// VideoService is just a mock
VideoService videoServiceMock = Mockito.mock(VideoService.class);
// Creating the real MyVideoService
MyVideoService myVideoService = new MyVideoService(videoServiceMock);
// Creating a spy proxy
MyVideoService myVideoServiceSpy = Mockito.spy(myVideoService);
ObjectThatUsesMyVideoService underTest = new ObjectThatUsesMyVideoService(myVideoServiceSpy);
underTest .deleteVideo("SomeValue");
// Verify that myVideoService was invoked
Mockito.verify(myVideoServiceSpy, Mockito.times(1)).delete("SomeValue");
// Verify that myVideoService invoked the VideoService
Mockito.verify(videoServiceMock, Mockito.times(1)).deleteVideo("SomeValue");
}
}