1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package biz.xsoftware.examples.socket; |
8 | |
|
9 | |
import java.io.IOException; |
10 | |
import java.nio.ByteBuffer; |
11 | |
|
12 | |
import junit.framework.TestCase; |
13 | |
import biz.xsoftware.mock.CalledMethod; |
14 | |
import biz.xsoftware.mock.MockObject; |
15 | |
import biz.xsoftware.mock.MockObjectFactory; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public class TestExample extends TestCase { |
24 | |
|
25 | |
private SysUnderTest sysUnderTest; |
26 | |
private MockObject mockSubsys; |
27 | |
private MockObject mockSocket; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public TestExample(String name) { |
33 | 1 | super(name); |
34 | 1 | } |
35 | |
|
36 | |
|
37 | |
|
38 | |
public void setUp() throws Exception { |
39 | 1 | mockSubsys = MockObjectFactory.createMock(OtherSubsystem.class); |
40 | 1 | mockSocket = MockObjectFactory.createMock(TCPSocket.class); |
41 | |
|
42 | 1 | sysUnderTest = new SysUnderTest((TCPSocket)mockSocket, (OtherSubsystem)mockSubsys); |
43 | 1 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
public void tearDown() throws IOException { |
48 | |
|
49 | 1 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public void testNetworkProblems() { |
60 | 1 | mockSocket.addThrowException("write", new IOException("problems writing to server")); |
61 | 1 | ByteBuffer buffer = ByteBuffer.allocate(100); |
62 | 1 | buffer.put(new byte[] { 1, 2, 3, 4}); |
63 | 1 | buffer.flip(); |
64 | 1 | mockSubsys.addReturnValue("getUserData", buffer); |
65 | |
|
66 | 1 | int id = 10; |
67 | 1 | sysUnderTest.sendUserToServer(10); |
68 | |
|
69 | 1 | CalledMethod[] methods = mockSubsys.expect("getUserData", "failedToSendUser"); |
70 | 1 | CalledMethod getUserData = methods[0]; |
71 | 1 | CalledMethod failedToSend = methods[1]; |
72 | |
|
73 | 1 | assertEquals(new Integer(id), getUserData.getAllParams()[0]); |
74 | 1 | assertEquals(new Integer(id), failedToSend.getParameter(0)); |
75 | 1 | } |
76 | |
|
77 | |
|
78 | |
} |