gpt4 book ai didi

java - 为什么 Mockito 不会抛出异常?

转载 作者:行者123 更新时间:2023-12-01 21:59:29 25 4
gpt4 key购买 nike

我有这个测试,来测试异常处理:

@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();

}

测试此代码:

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);
}

}

当在publishNew()中调用登录方法时,应该抛出XmlRpcException。相反,我收到一个错误:

Method PublishTest.testAddPageException()[pri:0, instance:com.report.publish.PublishTest@6b4886d3] should have thrown an exception of class org.apache.xmlrpc.XmlRpcException

我不明白为什么它不起作用。

最佳答案

您正在publishNew中捕获异常... } catch (XmlRpcException e) { LOGGER.error("发布到合流时出错", e); }”。因此测试失败。

关于java - 为什么 Mockito 不会抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33801543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com