Coverage Report - biz.xsoftware.examples.advanced.TestExample
 
Classes in this File Line Coverage Branch Coverage Complexity
TestExample
100%
20/20
N/A
0
 
 1  
 /*
 2  
  * Created on Jun 28, 2004
 3  
  *
 4  
  * To change the template for this generated file go to
 5  
  * Window - Preferences - Java - Code Generation - Code and Comments
 6  
  */
 7  
 package biz.xsoftware.examples.advanced;
 8  
 
 9  
 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  1
                 super(name);
 30  1
         }
 31  
 
 32  
         /**
 33  
          * @showcode
 34  
          */
 35  
         @Override
 36  
     public void setUp() {
 37  1
                 mockTaskSvc = MockObjectFactory.createMock(TaskSystemService.class);
 38  1
                 mockRecord = MockObjectFactory.createMock(TaskRecordService.class);
 39  
                 
 40  1
                 sysUnderTest = new SysUnderTestImpl((TaskRecordService)mockRecord, (TaskSystemService)mockTaskSvc);
 41  1
         }
 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  1
                 String expectedName = "joe";
 56  1
                 String expectedTask = "printpaper";
 57  1
                 sysUnderTest.sendUserMailWhenTaskStarted(expectedName, expectedTask);
 58  
                 
 59  1
                 Object o = mockTaskSvc.expect("addScheduleListener").getAllParams()[0];
 60  1
                 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  1
                 MockObject mockUser = MockObjectFactory.createMock(User.class);
 65  1
         mockUser.addReturnValue("addTaskDone", "foo");
 66  1
                 mockRecord.addReturnValue("getUser", mockUser);
 67  
                 
 68  
                 //have mockTaskSvc fire an event that should cause sysUnderTest
 69  
                 //to interact with mockMessaging
 70  1
                 l.eventStarted(null);
 71  
                 
 72  1
                 String actualUserId = (String)mockRecord.expect("getUser").getAllParams()[0];
 73  
                 
 74  1
                 assertEquals("User id was incorrect", expectedName, actualUserId);
 75  
                 
 76  1
                 String actualTask = (String)mockUser.expect("addTaskDone").getAllParams()[0];
 77  
                 
 78  1
                 assertEquals("task name was incorrect", expectedTask, actualTask);
 79  1
         }
 80  
 }