Coverage Report - biz.xsoftware.examples.basic.TestExample
 
Classes in this File Line Coverage Branch Coverage Complexity
TestExample
91%
40/44
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.basic;
 8  
 
 9  
 import java.util.logging.Logger;
 10  
 
 11  
 import junit.framework.TestCase;
 12  
 import biz.xsoftware.mock.CalledMethod;
 13  
 import biz.xsoftware.mock.MockObjectFactory;
 14  
 import biz.xsoftware.mock.MockObject;
 15  
 
 16  
 /**
 17  
  * This example has 3 tests.
 18  
  * 
 19  
  *  testBasicSunnyDay
 20  
  *  
 21  
  * This verifies that the correct amount of money was
 22  
  * taken from the credit card and taken from the correct
 23  
  * user.  It then verifies the correct amount of money was
 24  
  * put on the gift card
 25  
  * 
 26  
  * testFailureOfAuthorization
 27  
  * 
 28  
  * In this test we test that a failure from CreditAuthSvc
 29  
  * results in the correct StoreUnderTest behavior which in this
 30  
  * instance is to throw an exception to the user.
 31  
  * 
 32  
  * testFailureOfBuyingGiftCard
 33  
  * 
 34  
  * This tests to make sure if putting money on the gift card fails that
 35  
  * we will return money back to the users credit card.
 36  
  * @author Dean Hiller
 37  
  */
 38  
 public class TestExample extends TestCase {
 39  
 
 40  
         private SysUnderTest sysUnderTest;
 41  
         private MockObject mockCreditSvc;
 42  
         private MockObject mockGiftSvc;
 43  3
         private Logger log = Logger.getLogger(TestExample.class.getName());
 44  
         
 45  
         /**
 46  
          * @param name The name of the test
 47  
          * @showcode
 48  
          */
 49  
         public TestExample(String name) {
 50  3
                 super(name);
 51  3
         }
 52  
 
 53  
         /**
 54  
          * Setup for all the tests consists of created MockObjects that will simulate
 55  
          * the CreditAuthorizationSvc and GiftCardAccountSvc.  Then using 
 56  
          * dependency injection(inversion of control) pattern, these are given to
 57  
          * the SysUnderTest which has no idea they are not the real thing.
 58  
          * 
 59  
          * @showcode
 60  
          * @see junit.framework.TestCase#setUp()
 61  
          */
 62  
         @Override
 63  
     public void setUp() {
 64  
                 //first, create a mock CreditAuthorizationSvc
 65  3
                 mockCreditSvc = MockObjectFactory.createMock(CreditAuthorizationSvc.class);
 66  
                 //next, create a mock GiftCardAccountSvc
 67  3
                 mockGiftSvc = MockObjectFactory.createMock(GiftCardAccountSvc.class);
 68  
                 
 69  
                 //Create the system we are going to test by passing these mock objects
 70  
                 //into the constructor....
 71  3
                 CreditAuthorizationSvc credit = (CreditAuthorizationSvc)mockCreditSvc;
 72  3
                 GiftCardAccountSvc gift = (GiftCardAccountSvc)mockGiftSvc;
 73  3
                 sysUnderTest = new SysUnderTest(credit, gift);
 74  3
         }
 75  
         
 76  
         /**
 77  
          * This verifies that the correct amount of money was
 78  
          * taken from the credit card and taken from the correct
 79  
          * user.  It then verifies the correct amount of money was
 80  
          * put on the gift card
 81  
          * 
 82  
          * @see SysUnderTest#purchase
 83  
          * @showcode
 84  
          */
 85  
         public void testBasicSunnyDay() {                
 86  1
                 String user = "user1";
 87  1
                 double amount = 340.99;
 88  
                 
 89  
                 //now, purhase a gift card with user=user and amount=amount
 90  1
                 sysUnderTest.buyGiftCard(user, amount);
 91  
                 
 92  1
                 CalledMethod m1 = mockCreditSvc.expect("takeMoney");
 93  1
                 Object[] paramsToAuthorize = m1.getAllParams();
 94  
                 //method signature of takeMoney = 
 95  
                 //         public void takeMoney(String user, double usDollars);
 96  
                 //This first assert verifies that the first parameter pass into that was equal to user
 97  1
                 assertEquals("User should be the same", user, paramsToAuthorize[0]);
 98  
                 //This next assert verifies the second parameter passed to the
 99  
                 //takeMoney method was the amount....
 100  1
                 assertEquals("Amount should have been the same", new Double(amount), paramsToAuthorize[1]);
 101  
 
 102  
                 //lastly, we also expect putMoneyOnCard to be called with the correct amount...
 103  1
                 CalledMethod m2 = mockGiftSvc.expect("putMoneyOnCard");
 104  1
                 assertEquals("Money on gift card should be correct", amount, m2.getAllParams()[1]);
 105  1
         }
 106  
 
 107  
         /**
 108  
          * In this test we test that a failure from CreditAuthSvc
 109  
          * results in the correct StoreUnderTest behavior which in this
 110  
          * instance is to throw an exception to the user.
 111  
          *
 112  
          * @see SysUnderTest#purchase
 113  
          * @showcode
 114  
          */
 115  
         public void testFailureOfAuthorization() {
 116  
                 //we want to tell mockCreditSvc to simulate a failure
 117  
                 //by throwing an exception on the takeMoney method call
 118  1
                 mockCreditSvc.addThrowException("takeMoney", new IllegalStateException("cause failure"));
 119  
                 
 120  1
                 String user = "user1";
 121  1
                 double amount = 340.99;
 122  
                 try {
 123  
                         //in this case, we just expect an exception and nothing else...
 124  1
                         sysUnderTest.buyGiftCard(user, amount);
 125  0
                         fail("Expecting exactly the exact exception thrown here, but that didn't happen");
 126  1
                 } catch(IllegalStateException e) {
 127  1
                         log.info(e.getMessage());
 128  0
                 }
 129  1
         }
 130  
         
 131  
         /**
 132  
          * This tests to make sure if putting money on the gift card fails that
 133  
          * we will return money back to the users credit card.
 134  
          *
 135  
          * @see SysUnderTest#buyGiftCard 
 136  
          * @showcode
 137  
          */
 138  
         public void testFailureOfBuyingGiftCard() {        
 139  
                 //Now, we want to simulate a failure that should cause recovery to occur....
 140  1
                 mockGiftSvc.addThrowException("putMoneyOnCard", new RuntimeException("cause failure in subsystem"));
 141  
                 
 142  1
                 String user = "userXXX";
 143  1
                 double amount = 40.01;
 144  
                 try {
 145  
                         //Now call the system which should recover
 146  1
                         sysUnderTest.buyGiftCard(user, amount);
 147  0
                         fail("Should have thrown exception");
 148  1
                 } catch(PurchaseException e) {
 149  1
                         log.info(e.getMessage());
 150  0
                 }
 151  
                 
 152  
                 //make sure takeMoney then return money was called which should happen
 153  
                 //in our failure case.
 154  1
                 CalledMethod[] methods = mockCreditSvc.expect("takeMoney", "returnMoney");
 155  1
                 CalledMethod takeMoney = methods[0];
 156  1
                 assertEquals("Took from correct user", user, takeMoney.getParameter(0));
 157  1
                 assertEquals("Took 40 dollars", new Double(amount), takeMoney.getParameter(1));
 158  
                 
 159  1
                 CalledMethod returnMoney = methods[1];
 160  1
                 assertEquals("Gave money back to correct user", user, returnMoney.getParameter(0));
 161  1
                 assertEquals("amount put back on card is correct", new Double(amount), returnMoney.getParameter(1));
 162  1
         }
 163  
 }