Skip to content

Instantly share code, notes, and snippets.

@efleming969
Created December 2, 2014 22:03
Show Gist options
  • Save efleming969/fd5f69d529136cc5139d to your computer and use it in GitHub Desktop.
Save efleming969/fd5f69d529136cc5139d to your computer and use it in GitHub Desktop.
package example;
import java.util.*;
import java.io.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.*;
import org.junit.rules.*;
public class ExceptionRules {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void should_throw_exception_with_particular_message () throws Exception {
thrown.expect ( Exception.class );
thrown.expectMessage ( "bad data" );
Foo.someFunctionThatThrowsAnException ();
}
}
class Foo {
public static void someFunctionThatThrowsAnException ( ) throws Exception {
throw new Exception ( "bad data" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment