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