This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Core API
Luckydonald edited this page Oct 26, 2016
·
3 revisions
Core API is ment to be used for testing raw Java sockets. If you need too test HTTP connections, use the HTTP module.
Add the following dependency to your build system
<dependency>
<groupId>net.javacrumbs</groupId>
<artifactId>mock-socket-core</artifactId>
<version>0.9.0</version>
</dependency>
The API was inspired by Mockito framework. You have to start with static import:
import static net.javacrumbs.mocksocket.MockSocket.*;
Then you can write the test.
@Test
public void testResponse() throws Exception
{
byte[] mockData = new byte[]{1,2,3,4};
expectCall().andReturn(data(mockData));
Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
byte[] data = IOUtils.toByteArray(socket.getInputStream());
socket.close();
assertThat(data, is(mockData));
}
This test is not much useful since it tests that Java sockets work, but I hope you have the idea.
It's necessary to call MockSocket.reset() after each test.
@After
public void tearDown()
{
reset();
}
It's possible to compare the request data like this.
@Test
public void testRequest() throws Exception
{
byte[] dataToWrite = new byte[]{5,4,3,2};
expectCall().andReturn(emptyResponse());
Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
IOUtils.write(dataToWrite, socket.getOutputStream());
socket.close();
assertThat(recordedConnections().get(0), data(is(dataToWrite)));
assertThat(recordedConnections().get(0), address(is("example.org:1234")));
}
Sometimes it is not feasible to return mock responses based on the order of requests. In such situation, it's possible to use condition based mocks.
@Test
public void testConditionalAddress() throws Exception
{
byte[] mockData = new byte[]{1,2,3,4};
expectCall().andWhenRequest(address(is("example.org:1234"))).thenReturn(data(mockData));
Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
byte[] data = IOUtils.toByteArray(socket.getInputStream());
socket.close();
assertThat(data, is(mockData));
}
or
@Test
public void testConditionalData() throws Exception
{
byte[] dataToWrite = new byte[]{5,4,3,2};
byte[] mockData = new byte[]{1,2,3,4};
expectCall().andWhenRequest(data(is(dataToWrite))).thenReturn(data(mockData));
Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
IOUtils.write(dataToWrite, socket.getOutputStream());
byte[] data = IOUtils.toByteArray(socket.getInputStream());
socket.close();
assertThat(data, is(mockData));
}
As you have seen, Mock socket contains several built-in matchers.
Method | Description | Example |
---|---|---|
data(Matcher dataMatcher) | Compares data in andWhenRequest method | expectCall().andWhenRequest(data(is(dataToWrite))).thenReturn(data(mockData)) |
withData(InputStream data) | Compares data in andWhenRequest method | expectCall().andWhenRequest(withData(inputStream)).thenReturn(DATA1) |
address(Matcher addressMatcher) | Compares address in andWhenRequest method | expectCall().andWhenRequest(address(is("example.org:1234"))).thenReturn(data(mockData)); |
data(Matcher dataMatcher) | Compares response data | assertThat(recordedConnections().get(0), data(is(dataToWrite))); |