如何使用 Mockito 测试 void 方法
How to test a void method using Mockito
下面在代码部分发布的方法 returns 无效。我想知道如何测试 returns 无效的方法。
我检查了一些答案,但使用 "doThrow()" 传递异常,但在我的情况下,该方法不会抛出任何异常
请告诉我如何测试方法 returns 无效?
代码
public void setImageOnImageView(RequestCreator requestCreator, ImageView imgView) {
requestCreator.into(imgView);
}
测试:
public class ValidationTest {
@Mock
private Context mCtx = null;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Before
public void setUp() throws Exception {
mCtx = Mockito.mock(Context.class);
Assert.assertNotNull("Context is not null", mCtx);
}
@Test
public void setImageOnImageView() throws Exception {
Uri mockUri = mock(Uri.class);
RequestCreator requestCreator = Picasso.with(mCtx).load(mockUri);
RequestCreator spyRequestCreator = spy(requestCreator);
ImageView imageView = new ImageView(mCtx);
ImageView spyImageView = spy(imageView);
doThrow().when(spyRequestCreator).into(spyImageView);
//spyRequestCreator.into(spyImageView);
}
}
您到底要测试什么?那个测试是 "when setImageOnImageView(RequestCreator, ImageView)
is called, it invokes RequestCreator.into(ImageView)
"?
如果这就是您要测试的内容,那么您不想测试它 "returns void"。相反,我会推荐这样的东西:
@Test
public void itInvokesRequestCreatorIntoOnProvidedImageView() {
RequestCreator creator = mock(RequestCreator.class);
ImageView imageView = mock(ImageView.class);
setImageOnImageView(creator, imageView);
verify(creator).into(imageView);
}
如果你想验证一个 void 方法没有抛出任何东西,那么你可以使用 assertDoesNotThrows 方法如下
Assert that execution of the supplied executable does not throw any kind of exception.
Usage Note
Although any exception thrown from a test method will cause the test to fail, there are certain use cases where it can be beneficial to explicitly assert that an exception is not thrown for a given code block within a test method.
Assertions.assertDoesNotThrow(() -> testCl.voidM("test"...));
如果您的 class 中有一些依赖项,那么您可以使用验证来检查是否调用了该依赖项。
下面在代码部分发布的方法 returns 无效。我想知道如何测试 returns 无效的方法。 我检查了一些答案,但使用 "doThrow()" 传递异常,但在我的情况下,该方法不会抛出任何异常
请告诉我如何测试方法 returns 无效?
代码
public void setImageOnImageView(RequestCreator requestCreator, ImageView imgView) {
requestCreator.into(imgView);
}
测试:
public class ValidationTest {
@Mock
private Context mCtx = null;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Before
public void setUp() throws Exception {
mCtx = Mockito.mock(Context.class);
Assert.assertNotNull("Context is not null", mCtx);
}
@Test
public void setImageOnImageView() throws Exception {
Uri mockUri = mock(Uri.class);
RequestCreator requestCreator = Picasso.with(mCtx).load(mockUri);
RequestCreator spyRequestCreator = spy(requestCreator);
ImageView imageView = new ImageView(mCtx);
ImageView spyImageView = spy(imageView);
doThrow().when(spyRequestCreator).into(spyImageView);
//spyRequestCreator.into(spyImageView);
}
}
您到底要测试什么?那个测试是 "when setImageOnImageView(RequestCreator, ImageView)
is called, it invokes RequestCreator.into(ImageView)
"?
如果这就是您要测试的内容,那么您不想测试它 "returns void"。相反,我会推荐这样的东西:
@Test
public void itInvokesRequestCreatorIntoOnProvidedImageView() {
RequestCreator creator = mock(RequestCreator.class);
ImageView imageView = mock(ImageView.class);
setImageOnImageView(creator, imageView);
verify(creator).into(imageView);
}
如果你想验证一个 void 方法没有抛出任何东西,那么你可以使用 assertDoesNotThrows 方法如下
Assert that execution of the supplied executable does not throw any kind of exception. Usage Note Although any exception thrown from a test method will cause the test to fail, there are certain use cases where it can be beneficial to explicitly assert that an exception is not thrown for a given code block within a test method.
Assertions.assertDoesNotThrow(() -> testCl.voidM("test"...));
如果您的 class 中有一些依赖项,那么您可以使用验证来检查是否调用了该依赖项。