TestExample.java
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.fullapi;
08 
09 import junit.framework.TestCase;
10 import biz.xsoftware.mock.CalledMethod;
11 import biz.xsoftware.mock.MockObjectFactory;
12 import biz.xsoftware.mock.MockObject;
13 
14 /**
15  * JUnit Suite of TestCases for demonstrating mocking out a more
16  * complex api.
17  
18  @author Dean Hiller
19  */
20 public class TestExample extends TestCase {
21 
22   private EmailToPhone sysUnderTest;
23   private MockObject mockPhoneFactory;
24   private MockObject mockPhone;
25   
26   /**
27    * @showcode
28    */
29   public TestExample(String name) {
30     super(name);
31   }
32 
33   /**
34    * @showcode
35    */
36   @Override
37     public void setUp() {
38     mockPhoneFactory = MockObjectFactory.createMock(PhoneFactory.class);
39     mockPhone = MockObjectFactory.createMock(Phone.class);
40     
41     sysUnderTest = new EmailToPhoneImpl((PhoneFactory)mockPhoneFactory);
42   }
43   
44   /**
45    * Some api's can be more OO oriented then others and have no
46    * one facade into the whole subsystem as it may be too large, 
47    * so this demonstrates how even those api's can be mocked out.
48    
49    * This also demonstrates a susbystem that relies on two subsystems
50    * such that you can trigger an event in one subsystem and test
51    * the interaction with the other subsystem.
52    
53    * @showcode
54    */
55   public void testReceiveHighPriorityEmail() {
56         
57         //We know that when receivedHighPriorityEmail is called, the EmailToPhone system will
58         //create a Phone so we need to make sure the mock object's passes back a mockPhone
59         //that the test controls
60         mockPhoneFactory.addReturnValue("createPhone", mockPhone);
61         
62         String extension = "555-4444";
63     String emailTitle = "xxxx";
64         String emailContents = "yyyy";
65     sysUnderTest.receivedHighPriorityEmail(extension, emailTitle, emailContents);
66     
67         //we expect createPhone to be called
68     mockPhoneFactory.expect("createPhone");
69         
70         //we expect a phone call to be made to the correct extension
71     CalledMethod m = mockPhone.expect("makeCall");
72         assertEquals(extension, m.getAllParams()[0]);
73   }
74 }