【发布时间】:2016-08-10 16:06:40
【问题描述】:
我的测试用例有问题。
正在测试的方法有一个 try / catch 捕获 MalformedURLException 但在测试期间由于预期 MalformedURLException 的 Junit AssertionError 而失败。但我无法找出它实际上在抛出什么!这是我的代码(在 Eclipse 中创建为 MWE)。
我要测试的方法
public void throwMalFormedURLException(){
String s = new String("www.google.com");
try{
url = new URL(s);
}catch (Exception e){
e.printStackTrace();
e.getClass();
e.getCause();
}
}
测试方法
@Test (expected = MalformedURLException.class)
public void testThrowMalFormedURLException() {
MWE testClass = new MWE();
testClass.throwMalFormedURLException();
System.out.println("End of test");
}
这是控制台的输出
测试结束错误详细信息是:java.net.MalformedURLException:无协议:www.google.com
在 java.net.URL.(URL.java:593)
在 java.net.URL.(URL.java:490)
在 java.net.URL.(URL.java:439)
在 MWE.throwMalFormedURLException(MWE.java:12)
在 testMWE.testThrowMalFormedURLException(testMWE.java:12)
在 Junit 控制台中它说:
java.lang.AssertionError:预期异常:Java.net.MalformedURLException
但 Junit 报告失败,即使控制台告诉我我遇到了 MalformedURLException。
我在这个测试中做错了什么?
感谢您的意见。
大卫
【问题讨论】: