Created
June 7, 2014 01:05
-
-
Save dadarek/d0bcf790de45ced661c1 to your computer and use it in GitHub Desktop.
Cornering dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Socket { | |
void send(String string); | |
void close(); | |
} | |
class ClientHandler { | |
private Socket socket; | |
public ClientHandler( Socket socket ){ | |
this.socket = socket; | |
} | |
public void handle(){ | |
socket.send( "hello world" ); | |
socket.close(); | |
} | |
public void someOtherMethod( SomeOtherClass o ){ | |
o.hi(); | |
} | |
} | |
class RealSocket implements Socket { | |
private java.net.Socket socket; | |
public RealSocket(java.net.Socket socket){ | |
this.socket = socket; | |
} | |
void send(String string){ | |
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true); | |
out.println(string); | |
} | |
void close(){ | |
this.socket.close(); | |
} | |
} | |
class FakeSocket implements Socket { | |
public String whatWasSent; | |
public Boolean wasClosed; | |
public Boolean throwExceptionOnSend; | |
public FakeSocket(){ | |
this.wasClosed = false; | |
this.throwExceptionOnSend = false; | |
} | |
void send(String string){ | |
if(throwExceptionOnSend){ | |
throw new IOException(); | |
} | |
this.whatWasSent = string; | |
} | |
void close(){ | |
this.wasClosed = true; | |
} | |
} | |
class ClientHandlerTest { | |
@Test | |
void testSendingHelloWorld(){ | |
FakeSocket fakeSocket = new FakeSocket(); | |
ClientHandler handler = ClientHandler.new(fakeSocket); | |
handler.handle(); | |
expect(fakeSocket.whatWasSent).toEqual("Hello World"); | |
expect(fakeSocket.wasClosed).toEqual(true); | |
} | |
@Test | |
void testNetworkException(){ | |
FakeSocket fakeSocket = new FakeSocket(); | |
fakeSocket.throwExceptionOnSend = true; | |
expect { | |
handler.handle(); | |
}.toNotThrow | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClientHandler | |
def initialize( client ) | |
@client = client | |
end | |
def handle | |
@client.send( 'Hello World' ) | |
@client.close | |
end | |
end | |
describe ClientHandler do | |
it 'sends "Hello World" to the client' do | |
fake_client = double(:client) | |
fake_client.should_receive(:send).with('Hello World') | |
fake_client.should_receive(:close) | |
ClientHandler.new(fake_client).handle | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment