Skip to content

Instantly share code, notes, and snippets.

@ahirata
Created July 1, 2012 11:32
Show Gist options
  • Select an option

  • Save ahirata/3028071 to your computer and use it in GitHub Desktop.

Select an option

Save ahirata/3028071 to your computer and use it in GitHub Desktop.
Using junit ExpectedException instead of @test(expected=SomeException.class)
package atarih.lab;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class TestWithRule {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void expectedException() {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("my");
expectedException.expectMessage("my.exception");
new Foo().bar();
}
public static class Foo {
public void bar() {
throw new IllegalArgumentException("my.exception");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment