001 /*
002 * Created on Jun 28, 2004
003 *
004 * To change the template for this generated file go to
005 * Window - Preferences - Java - Code Generation - Code and Comments
006 */
007 package biz.xsoftware.examples.basic;
008
009 import junit.framework.TestCase;
010 import biz.xsoftware.mock.CalledMethod;
011 import biz.xsoftware.mock.MockObjectFactory;
012 import biz.xsoftware.mock.MockObject;
013
014 /**
015 * This example has 3 tests.
016 *
017 * testBasicSunnyDay
018 *
019 * This verifies that the correct amount of money was
020 * taken from the credit card and taken from the correct
021 * user. It then verifies the correct amount of money was
022 * put on the gift card
023 *
024 * testFailureOfAuthorization
025 *
026 * In this test we test that a failure from CreditAuthSvc
027 * results in the correct StoreUnderTest behavior which in this
028 * instance is to throw an exception to the user.
029 *
030 * testFailureOfBuyingGiftCard
031 *
032 * This tests to make sure if putting money on the gift card fails that
033 * we will return money back to the users credit card.
034 * @author Dean Hiller
035 */
036 public class TestExample extends TestCase {
037
038 private SysUnderTest sysUnderTest;
039 private MockObject mockCreditSvc;
040 private MockObject mockGiftSvc;
041
042 /**
043 * @param name The name of the test
044 * @showcode
045 */
046 public TestExample(String name) {
047 super(name);
048 }
049
050 /**
051 * Setup for all the tests consists of created MockObjects that will simulate
052 * the CreditAuthorizationSvc and GiftCardAccountSvc. Then using
053 * dependency injection(inversion of control) pattern, these are given to
054 * the SysUnderTest which has no idea they are not the real thing.
055 *
056 * @showcode
057 * @see junit.framework.TestCase#setUp()
058 */
059 @Override
060 public void setUp() {
061 //first, create a mock CreditAuthorizationSvc
062 mockCreditSvc = MockObjectFactory.createMock(CreditAuthorizationSvc.class);
063 //next, create a mock GiftCardAccountSvc
064 mockGiftSvc = MockObjectFactory.createMock(GiftCardAccountSvc.class);
065
066 //Create the system we are going to test by passing these mock objects
067 //into the constructor....
068 CreditAuthorizationSvc credit = (CreditAuthorizationSvc)mockCreditSvc;
069 GiftCardAccountSvc gift = (GiftCardAccountSvc)mockGiftSvc;
070 sysUnderTest = new SysUnderTest(credit, gift);
071 }
072
073 /**
074 * This verifies that the correct amount of money was
075 * taken from the credit card and taken from the correct
076 * user. It then verifies the correct amount of money was
077 * put on the gift card
078 *
079 * @see SysUnderTest#purchase
080 * @showcode
081 */
082 public void testBasicSunnyDay() {
083 String user = "user1";
084 double amount = 340.99;
085
086 //now, purhase a gift card with user=user and amount=amount
087 sysUnderTest.buyGiftCard(user, amount);
088
089 CalledMethod m1 = mockCreditSvc.expect("takeMoney");
090 Object[] paramsToAuthorize = m1.getAllParams();
091 //method signature of takeMoney =
092 // public void takeMoney(String user, double usDollars);
093 //This first assert verifies that the first parameter pass into that was equal to user
094 assertEquals("User should be the same", user, paramsToAuthorize[0]);
095 //This next assert verifies the second parameter passed to the
096 //takeMoney method was the amount....
097 assertEquals("Amount should have been the same", new Double(amount), paramsToAuthorize[1]);
098
099 //lastly, we also expect putMoneyOnCard to be called with the correct amount...
100 CalledMethod m2 = mockGiftSvc.expect("putMoneyOnCard");
101 assertEquals("Money on gift card should be correct", amount, m2.getAllParams()[1]);
102 }
103
104 /**
105 * In this test we test that a failure from CreditAuthSvc
106 * results in the correct StoreUnderTest behavior which in this
107 * instance is to throw an exception to the user.
108 *
109 * @see SysUnderTest#purchase
110 * @showcode
111 */
112 public void testFailureOfAuthorization() {
113 //we want to tell mockCreditSvc to simulate a failure
114 //by throwing an exception on the takeMoney method call
115 mockCreditSvc.addThrowException("takeMoney", new IllegalStateException("cause failure"));
116
117 String user = "user1";
118 double amount = 340.99;
119 try {
120 //in this case, we just expect an exception and nothing else...
121 sysUnderTest.buyGiftCard(user, amount);
122 fail("Expecting exactly the exact exception thrown here, but that didn't happen");
123 } catch(IllegalStateException e) {}
124 }
125
126 /**
127 * This tests to make sure if putting money on the gift card fails that
128 * we will return money back to the users credit card.
129 *
130 * @see SysUnderTest#buyGiftCard
131 * @showcode
132 */
133 public void testFailureOfBuyingGiftCard() {
134 //Now, we want to simulate a failure that should cause recovery to occur....
135 mockGiftSvc.addThrowException("putMoneyOnCard", new RuntimeException("cause failure in subsystem"));
136
137 String user = "userXXX";
138 double amount = 40.01;
139 try {
140 //Now call the system which should recover
141 sysUnderTest.buyGiftCard(user, amount);
142 fail("Should have thrown exception");
143 } catch(PurchaseException e) {}
144
145 //make sure takeMoney then return money was called which should happen
146 //in our failure case.
147 CalledMethod[] methods = mockCreditSvc.expect("takeMoney", "returnMoney");
148 CalledMethod takeMoney = methods[0];
149 assertEquals("Took from correct user", user, takeMoney.getParameter(0));
150 assertEquals("Took 40 dollars", new Double(amount), takeMoney.getParameter(1));
151
152 CalledMethod returnMoney = methods[1];
153 assertEquals("Gave money back to correct user", user, returnMoney.getParameter(0));
154 assertEquals("amount put back on card is correct", new Double(amount), returnMoney.getParameter(1));
155 }
156 }
|