01 /*
02 * Created on Jun 28, 2004
03 *
04 * To change the template for this generated file go to
05 * Window - Preferences - Java - Code Generation - Code and Comments
06 */
07 package biz.xsoftware.examples.advanced;
08
09 import junit.framework.TestCase;
10 import biz.xsoftware.mock.MockObjectFactory;
11 import biz.xsoftware.mock.MockObject;
12
13 /**
14 * JUnit Suite of TestCases for demonstrating mocking out a more
15 * complex api.
16 *
17 * @author Dean Hiller
18 */
19 public class TestExample extends TestCase {
20
21 private SysUnderTest sysUnderTest;
22 private MockObject mockTaskSvc;
23 private MockObject mockRecord;
24
25 /**
26 * @showcode
27 */
28 public TestExample(String name) {
29 super(name);
30 }
31
32 /**
33 * @showcode
34 */
35 @Override
36 public void setUp() {
37 mockTaskSvc = MockObjectFactory.createMock(TaskSystemService.class);
38 mockRecord = MockObjectFactory.createMock(TaskRecordService.class);
39
40 sysUnderTest = new SysUnderTestImpl((TaskRecordService)mockRecord, (TaskSystemService)mockTaskSvc);
41 }
42
43 /**
44 * Some api's can be more OO oriented then others and have no
45 * one facade into the whole subsystem as it may be too large,
46 * so this demonstrates how even those api's can be mocked out.
47 *
48 * This also demonstrates a susbystem that relies on two subsystems
49 * such that you can trigger an event in one subsystem and test
50 * the interaction with the other subsystem.
51 *
52 * @showcode
53 */
54 public void testSystemInteractionWithSubsytemsAPI() {
55 String expectedName = "joe";
56 String expectedTask = "printpaper";
57 sysUnderTest.sendUserMailWhenTaskStarted(expectedName, expectedTask);
58
59 Object o = mockTaskSvc.expect("addScheduleListener").getAllParams()[0];
60 ScheduleListener l = (ScheduleListener)o;
61
62 //set an object to return from mockRecord as we know we are testing
63 //that a call to getUser results from an event
64 MockObject mockUser = MockObjectFactory.createMock(User.class);
65 mockUser.addReturnValue("addTaskDone", "foo");
66 mockRecord.addReturnValue("getUser", mockUser);
67
68 //have mockTaskSvc fire an event that should cause sysUnderTest
69 //to interact with mockMessaging
70 l.eventStarted(null);
71
72 String actualUserId = (String)mockRecord.expect("getUser").getAllParams()[0];
73
74 assertEquals("User id was incorrect", expectedName, actualUserId);
75
76 String actualTask = (String)mockUser.expect("addTaskDone").getAllParams()[0];
77
78 assertEquals("task name was incorrect", expectedTask, actualTask);
79 }
80 }
|