无法测试 class return customException
Cannot test a class that return a customException
在试验 JUnit 时,我正在尝试测试一个简单的私有方法,如下所示,该方法接收一个字符串并确保它不包含单词 'Dummy'。
我知道,可以将测试放在与 class 相同的包 中,并将方法的访问修饰符更改为包,但我想用反思来学习。
private void validateString(String myString) throws CustomException {
if (myString.toLowerCase().matches(".*dummy.*"))
throw new CustomException("String has the invalid word!");
}
我试图通过反射访问私有方法,但测试失败!它显示以下异常:
java.lang.AssertionError:Expected test to throw
(an instance of com.myproject.exception.CustomException and exception
with message a string containing "String has the invalid word!")
根据这个 question 的回答,我也在抓取 InvocationTargetException
。
JUnit
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldThrowExceptionForInvalidString() {
thrown.expect(CustomException.class);
thrown.expectMessage("String has the invalid word!");
try {
MyClass myCls = new MyClass();
Method valStr = myCls.getClass().getDeclaredMethod(
"validateString", String.class);
valStr.setAccessible(true);
valStr.invoke(myCls, "This is theDummyWord find it if you can.");
} catch (InvocationTargetException | NoSuchMethodException
| SecurityException | IllegalAccessException
| IllegalArgumentException n) {
if (n.getCause().getClass() == CustomException.class) {
throw new CustomException("String has the invalid word!");
}
}
}
我同意上面评论中@Stultuske 的观点,并将测试重写为:
@Test
public void shouldThrowExceptionForInvalidString() {
try {
MyClass myCls = new MyClass();
Method valStr = myCls.getClass().getDeclaredMethod(
"validateString", String.class);
valStr.setAccessible(true);
valStr.invoke(myCls, "This is theDummyWord find it if you can.");
} catch (Exception e) {
assert(e instanceOf CustomException);
assert(e.getMessage.equals("String has the invalid word!"));
}
}
或者如果你想使用 ExpectedException
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldThrowExceptionForInvalidString() {
thrown.expect(CustomException.class);
thrown.expectMessage("String has the invalid word!");
MyClass myCls = new MyClass();
Method valStr = myCls.getClass().getDeclaredMethod("validateString", String.class);
valStr.setAccessible(true);
valStr.invoke(myCls, "This is theDummyWord find it if you can.");
}
在试验 JUnit 时,我正在尝试测试一个简单的私有方法,如下所示,该方法接收一个字符串并确保它不包含单词 'Dummy'。
我知道,可以将测试放在与 class 相同的包 中,并将方法的访问修饰符更改为包,但我想用反思来学习。
private void validateString(String myString) throws CustomException {
if (myString.toLowerCase().matches(".*dummy.*"))
throw new CustomException("String has the invalid word!");
}
我试图通过反射访问私有方法,但测试失败!它显示以下异常:
java.lang.AssertionError:Expected test to throw
(an instance of com.myproject.exception.CustomException and exception
with message a string containing "String has the invalid word!")
根据这个 question 的回答,我也在抓取 InvocationTargetException
。
JUnit
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldThrowExceptionForInvalidString() {
thrown.expect(CustomException.class);
thrown.expectMessage("String has the invalid word!");
try {
MyClass myCls = new MyClass();
Method valStr = myCls.getClass().getDeclaredMethod(
"validateString", String.class);
valStr.setAccessible(true);
valStr.invoke(myCls, "This is theDummyWord find it if you can.");
} catch (InvocationTargetException | NoSuchMethodException
| SecurityException | IllegalAccessException
| IllegalArgumentException n) {
if (n.getCause().getClass() == CustomException.class) {
throw new CustomException("String has the invalid word!");
}
}
}
我同意上面评论中@Stultuske 的观点,并将测试重写为:
@Test
public void shouldThrowExceptionForInvalidString() {
try {
MyClass myCls = new MyClass();
Method valStr = myCls.getClass().getDeclaredMethod(
"validateString", String.class);
valStr.setAccessible(true);
valStr.invoke(myCls, "This is theDummyWord find it if you can.");
} catch (Exception e) {
assert(e instanceOf CustomException);
assert(e.getMessage.equals("String has the invalid word!"));
}
}
或者如果你想使用 ExpectedException
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldThrowExceptionForInvalidString() {
thrown.expect(CustomException.class);
thrown.expectMessage("String has the invalid word!");
MyClass myCls = new MyClass();
Method valStr = myCls.getClass().getDeclaredMethod("validateString", String.class);
valStr.setAccessible(true);
valStr.invoke(myCls, "This is theDummyWord find it if you can.");
}