[英]Why will Mockito not throw an exception?
I have this test, to test exception handling: 我有此测试,以测试异常处理:
@BeforeTest
public void setup() throws XmlRpcException {
confluence1 = Mockito.mock(XWikiXmlRpcClient.class);
confluence2 = Mockito.mock(XWikiXmlRpcClient.class);
XWikiPage testPage = new XWikiPage();
testPage.setContent("Test");
testPage.setTitle("Page Title");
testPage.setUrl("http://confluence:8080/Test");
Mockito.when(confluence1.storePage(Mockito.any(Page.class))).thenReturn(testPage);
Mockito.when(confluence2.getPage(Mockito.anyString())).thenReturn(testPage);
publish = new Confluence(confluence1, confluence2);
}
@Test(expectedExceptions = XmlRpcException.class)
public void testAddPageException() throws XmlRpcException {
Mockito.doThrow(new XmlRpcException("Error")).when(confluence1).
login(Mockito.anyString(), Mockito.anyString());
publish.publishNew();
} }
testing this code: 测试此代码:
public void publishNew() throws XmlRpcException {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date today = Calendar.getInstance().getTime();
try {
if (parentPageId == null) {
LOGGER.error("Unable to publish to confluence - required page id of parent confluence page is not set");
}
LOGGER.info("Logging in to Confluence");
rpcConfluence1.login(userName, passWord);
Page page = new Page();
page.setSpace(owrConf.getString(ConfigKeys.CONFLUENCE_SPACE));
page.setTitle(owrConf.getString(ConfigKeys.CONFLUENCE_NEW_PAGE_TITLE) + "_" + df.format(today));
LOGGER.info("Adding new page: " + page.getTitle() + " to space " + page.getSpace());
page.setContent(reformatMarkup());
page.setParentId(parentPageId);
Page newPage = rpcConfluence1.storePage(page);
LOGGER.info("New page: " + newPage.getUrl());
savePageUrl(newPage);
} catch (XmlRpcException e) {
LOGGER.error("Error publishing to confluence", e);
}
}
It is supposed to throw and XmlRpcException when the login method is called in publishNew(). 在publishNew()中调用登录方法时,应该抛出XmlRpcException和。 Instead I get an error: 相反,我得到一个错误:
Method PublishTest.testAddPageException()[pri:0, instance:com.report.publish.PublishTest@6b4886d3] should have thrown an exception of class org.apache.xmlrpc.XmlRpcException
I can't figure out why it's not working. 我不知道为什么它不起作用。
You are catching the exception in publishNew ... } catch (XmlRpcException e) { LOGGER.error("Error publishing to confluence", e); }
您正在publishNew ...中捕获异常。 } catch (XmlRpcException e) { LOGGER.error("Error publishing to confluence", e); }
} catch (XmlRpcException e) { LOGGER.error("Error publishing to confluence", e); }
". because of that the test fails. } catch (XmlRpcException e) { LOGGER.error("Error publishing to confluence", e); }
“。因此测试失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.