我正在尝试使用Selenium 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)));
}
你不是在砸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));