Skip to content

Instantly share code, notes, and snippets.

@jamesshore
Created May 30, 2010 07:36
Show Gist options
  • Save jamesshore/418861 to your computer and use it in GitHub Desktop.
Save jamesshore/418861 to your computer and use it in GitHub Desktop.
@RunWith(JMock.class)
public class _Rot13StringTest {
private Mockery _mockery = new JUnit4Mockery();
private PersistenceMechanism _fileSystem;
@Before
public void setup() {
_fileSystem = _mockery.mock(PersistenceMechanism.class);
}
@Test
public void saveAs() throws IOException {
TransformableString string = new Rot13String("abc", _fileSystem);
_mockery.checking(new Expectations() {{
oneOf (_fileSystem).overwrite("filename", "abc");
}});
string.saveAs("filename");
}
@Test
public void writeTo() {
TransformableString string = new Rot13String("foo", _fileSystem);
final Display display = _mockery.mock(Display.class);
_mockery.checking(new Expectations() {{
oneOf (display).write("foo");
}});
string.writeTo(display);
}
@Test
public void go_handlesIOExceptionNicely() throws IOException {
_mockery.checking(new Expectations() {{
oneOf (_stringFactory).load(_inputFile);
will(throwException(new FileNotFoundException("file not found")));
oneOf (_display).write("file not found");
}});
_ui.go(new String[] {_inputFile, _outputFile});
}
// You might expect it to look like this:
public static Rot13String load(String filename) throws IOException {
return new Rot13String(_fileSystem.read(filename));
}
public void saveAs(String filename) throws IOException {
_fileSystem.overwrite(filename, _string);
}
public void saveAs(String filename) throws IOException {
_fileSystem.overwrite(filename, _string);
}
// But instead it looks like this:
public TransformableString load(String filename) throws IOException {
return new Rot13String(_fileSystem.read(filename), _fileSystem);
}
public UI(PrintStream out, Configuration configuration) {
this(new ConsoleDisplay(out), new Rot13StringFileSystemLoader(
new FileSystem(configuration)));
}
public static void main(String[] args) {
main(args, System.out, Configuration.production());
}
public static void main(String[] args, PrintStream out, Configuration configuration) {
new UI(out, configuration).go(args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment