lundi 5 janvier 2015

JUnit - expected exception message regular expression

I'm switching from TestNg to JUnit. I need to match expected exception message with predefined regular expression such as in the following TestNg code:



@Test(expectedExceptions = SomeClass.class, expectedExceptionsMessageRegExp = "**[123]regexExample*")
public void should_throw_exception_when_...() throws IOException {
generatesException();
}


That works fine, but I cannot achieve the same behavior in JUnit. I came up with this solution:



@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void should_throw_exception_when_...() throws IOException {
expectedEx.expect(SomeClass.class);
expectedEx.expectMessage("**[123]regexExample*");
generatesException();
}


But method expectedEx.expectMessage("**[123]regexExample*"); does not support regular expressions, I need to provide it with the exact hardcoded message. I've seen this could be achieved by providing that method with Matcher but I'm not sure how to do it properly.


Any good way of doing this?


Aucun commentaire:

Enregistrer un commentaire