I have the following class:
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);
}
the problem is that the expectMessage in Test 2 is false, for the sake of discussion the actual expected message is exceptionString 3, and yet the test doesn't fail, although the expected message differs from the actual message. It gets weirder - if I provide the expectMessage a gibberish message such as "fdsfsdfsdfsdfsdfsdthe" - then the test does fail:
java.lang.AssertionError: Expected: (an instance of InvalidParamException and exception with message a string containing "fdsfsdfsdfsdfsdfsdthe") but: exception with message a string containing "fdsfsdfsdfsdfsdfsdthe" message was "exceptionMessage3"
And that's the expected behavior - but when I change the exceptionMessage3 just a little bit, letter or two, even sending an empty string - the the test does not fail at all.
Am I missing something?
ExpectedException only holds 1 expected exception.
You can't reuse it like this. Either:
ExpectedException instances;try/catch;assertThrows (or expectThrows) if you're using a recent-enough version of Java (8+) and JUnit that supports it.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With