【发布时间】:2023-01-10 03:15:51
【问题描述】:
我有一个 ServerConnection.java 类,它有以下方法
private String getUrl() throws MalformedURLException {
// some operations and condition
URL url = getDNSBasedUrl();
}
public String getDNSBasedUrl() throws MalformedURLException{
if(this.nameSpace==null)
throw new MalformedURLException("undefined namespace");
return this.nodeName + this.nameSpace;
}
测试用例编写如下
@Test(expected = MalformedURLException.class)
public void gctNameSpace_Exception(){
ServerConnection connection = new ServerConnection();
connection.setNameSpace(null);
String s = connection.getDNSBasedUrl();
}
我期待 MalformedURLException 但低于错误。
java.lang.Exception: Unexpected exception, expected<java.net.MalformedURLException> but was<java.lang.reflect.UndeclaredThrowableException>
不想更改方法抛出的异常,getUrl() 在许多地方被引用。 提前致谢。
【问题讨论】:
标签: java exception junit mockito malformedurlexception