Skip to content

Instantly share code, notes, and snippets.

@muga
Created January 30, 2013 05:48
Show Gist options
  • Save muga/4671001 to your computer and use it in GitHub Desktop.
Save muga/4671001 to your computer and use it in GitHub Desktop.
sample with mockito
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import org.junit.Test;
public class Sample {
static class Reader {
private String fileName;
public Reader() {
}
public void init(String fileName) {
this.fileName = fileName;
readHeader();
}
public boolean readHeader() {
return true;
}
}
@Test
public void sample() throws Exception {
//Reader orig = new Reader();
//Reader reader = spy(orig);
Reader reader = mock(Reader.class);
when(reader.readHeader()).thenThrow(new RuntimeException());
reader.init("theFile"); // RuntimeException is not thrown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment