我正在尝试使用硒Webelement作为参数使该功能的测试用例成为重要案例。
我正在尝试模拟元素,但是此测试用例给出了错误。我要作为测试用例的方法是这样。
@Override
public boolean isDownloadStarted(WebDriver driver) {
boolean isDownloadStarted = false;
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
if (tabs.size() == 1) {
isDownloadStarted = true;
}
return isDownloadStarted;
}
测试用例给出了空指针异常
DownloadStatusListenerImpl status;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
status = new DownloadStatusListenerImpl();
}
@Test
public void testDownloadStatusListenerImpl() {
Mockito.when(status.isDownloadStarted(Mockito.any(WebDriver.class))).thenReturn(true);
assertEquals(true, status.isDownloadStarted(Mockito.any(WebDriver.class)));
}
答案 0 :(得分:1)
您并没有存根status
。您可以在其中添加@Spy
注释(并停止覆盖它):
@Spy // Annotation added here
DownloadStatusListenerImpl status;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
// Stopped overwriting status here
}
或者您可以显式调用Mockito.spy
:
@Before
public void before() {
status = Mockito.spy(new DownloadStatusListenerImpl());
}
编辑:
在这样的方法上调用when
仍然会调用它,因此会失败。您需要改为使用doReturn
语法:
Mockito.doReturn(true).when(status).isDownloadStarted(Mockito.any(WebDriver.class));