Java Junit 4 ExpectedException.expectMessage应该无法通过测试,并且它不';T
我有以下课程:Java Junit 4 ExpectedException.expectMessage应该无法通过测试,并且它不';T,java,exception,junit,java-8,Java,Exception,Junit,Java 8,我有以下课程: public class MyClassTest { @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testMyMethod() throws Exception { // Test 1 String[] args = ...; MyClass.myMethod(args); // Test 2 thrown.exp
public class MyClassTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testMyMethod() throws Exception {
// Test 1
String[] args = ...;
MyClass.myMethod(args);
// Test 2
thrown.expect(InvalidParamException.class);
thrown.expectMessage("exceptionString 1");
args = ...;
MyClass.myMethod(args);
// test 3
thrown.expect(InvalidParamException.class);
thrown.expectMessage("exceptionString 2");
args = ...;
MyClass.myMethod(args);
}
问题在于,测试2中的expectMessage为false,为了便于讨论,实际的预期消息为exceptionString 3,但是测试没有失败,尽管预期消息与实际消息不同。
更奇怪的是,如果我给expectMessage提供了一条乱七八糟的消息,比如“fdsfsdfsdfsdthe”,那么测试就失败了:
java.lang.AssertionError:
应为:(InvalidParamException的实例和包含“fdsfsdfsdfsdthe”字符串的异常消息)
但是:消息异常包含“fdsfsdfsdfsdthe”的字符串消息是“exceptionMessage3”
这是预期的行为-但是当我稍微更改exceptionMessage3时,一两个字母,甚至发送一个空字符串-测试根本不会失败
我遗漏了什么吗?
ExpectedException
仅包含1个预期异常
你不能像这样重复使用它。要么:
ExpectedException
实例李>
尝试
/捕获
李>
expectThrows
)ExpectedException
仅包含1个预期异常。你不能像这样重复使用它。我用的是第一个,那就是it@SockworkOrange我猜你现在指的是第二个;我刚刚编辑添加了一个新的第一个。