Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/basic
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv30309/input/javasrc/biz/xsoftware/examples/basic
Modified Files:
SysUnderTest.java TestExample.java CreditAuthorizationSvc.java
Log Message:
add more to the examples.
Index: TestExample.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/basic/TestExample.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** TestExample.java 10 Sep 2006 18:44:06 -0000 1.3
--- TestExample.java 11 Sep 2006 03:47:39 -0000 1.4
***************
*** 13,18 ****
/**
! * This is the class actually holding the test Examples for you to look at.
*
* @author Dean Hiller
*/
--- 13,35 ----
/**
! * This example has 3 tests.
! *
! * testBasicSunnyDay
! *
! * This verifies that the correct amount of money was
! * taken from the credit card and taken from the correct
! * user. It then verifies the correct amount of money was
! * put on the gift card
! *
! * testFailureOfAuthorization
! *
! * In this test we test that a failure from CreditAuthSvc
! * results in the correct StoreUnderTest behavior which in this
! * instance is to throw an exception to the user.
*
+ * testFailureOfBuyingGiftCard
+ *
+ * This tests to make sure if putting money on the gift card fails that
+ * we will return money back to the users credit card.
* @author Dean Hiller
*/
***************
*** 32,35 ****
--- 49,57 ----
/**
+ * Setup for all the tests consists of created MockObjects that will simulate
+ * the CreditAuthorizationSvc and GiftCardAccountSvc. Then using
+ * dependency injection(inversion of control) pattern, these are given to
+ * the SysUnderTest which has no idea they are not the real thing.
+ *
* @showcode
* @see junit.framework.TestCase#setUp()
***************
*** 37,43 ****
--- 59,69 ----
@Override
public void setUp() {
+ //first, create a mock CreditAuthorizationSvc
mockCreditSvc = MockObjectFactory.createMock(CreditAuthorizationSvc.class);
+ //next, create a mock GiftCardAccountSvc
mockGiftSvc = MockObjectFactory.createMock(GiftCardAccountSvc.class);
+ //Create the system we are going to test by passing these mock objects
+ //into the constructor....
CreditAuthorizationSvc credit = (CreditAuthorizationSvc)mockCreditSvc;
GiftCardAccountSvc gift = (GiftCardAccountSvc)mockGiftSvc;
***************
*** 46,64 ****
/**
! * Test that when we purchase an item from the SysUnderTest
! * that the CreditAuthorization Svc is used to verify the
! * customer has that amount of money on his credit card.
*
* @see SysUnderTest#purchase
* @showcode
*/
! public void testBasicSysUnderTest() {
String user = "user1";
double amount = 340.99;
- sysUnderTest.purchase("itemA", user, amount);
! Object[] paramsToAuthorize = mockCreditSvc.expect("authorize").getAllParams();
assertEquals("User should be the same", user, paramsToAuthorize[0]);
assertEquals("Amount should have been the same", new Double(amount), paramsToAuthorize[1]);
}
--- 72,103 ----
/**
! * This verifies that the correct amount of money was
! * taken from the credit card and taken from the correct
! * user. It then verifies the correct amount of money was
! * put on the gift card
*
* @see SysUnderTest#purchase
* @showcode
*/
! public void testBasicSunnyDay() {
String user = "user1";
double amount = 340.99;
! //now, purhase a gift card with user=user and amount=amount
! sysUnderTest.buyGiftCard(user, amount);
!
! CalledMethod m1 = mockCreditSvc.expect("takeMoney");
! Object[] paramsToAuthorize = m1.getAllParams();
! //method signature of takeMoney =
! // public void takeMoney(String user, double usDollars);
! //This first assert verifies that the first parameter pass into that was equal to user
assertEquals("User should be the same", user, paramsToAuthorize[0]);
+ //This next assert verifies the second parameter passed to the
+ //takeMoney method was the amount....
assertEquals("Amount should have been the same", new Double(amount), paramsToAuthorize[1]);
+
+ //lastly, we also expect putMoneyOnCard to be called with the correct amount...
+ CalledMethod m2 = mockGiftSvc.expect("putMoneyOnCard");
+ assertEquals("Money on gift card should be correct", amount, m2.getAllParams()[1]);
}
***************
*** 72,92 ****
*/
public void testFailureOfAuthorization() {
! mockCreditSvc.addThrowException(new RuntimeException("test robustness of StoreUnderTest"), "authorize");
String user = "user1";
double amount = 340.99;
try {
! sysUnderTest.purchase("itemA", user, amount);
! fail("Expecting exactly the IllegalStateException for some odd reason(didn't want to define my own)");
! } catch(PurchaseException e) {}
}
/**
! * Test that
! * <ol>
! * <li>Money is taken from users credit account</li>
! * <li>Put money on card, but cause failure</li>
! * <li>Assert that money is put back in users credit account</li>
! * </ol>
*
* @see SysUnderTest#buyGiftCard
--- 111,130 ----
*/
public void testFailureOfAuthorization() {
! //we want to tell mockCreditSvc to simulate a failure
! //by throwing an exception on the takeMoney method call
! mockCreditSvc.addThrowException(new IllegalStateException("cause failure"), "takeMoney");
String user = "user1";
double amount = 340.99;
try {
! //in this case, we just expect an exception and nothing else...
! sysUnderTest.buyGiftCard(user, amount);
! fail("Expecting exactly the exact exception thrown here, but that didn't happen");
! } catch(IllegalStateException e) {}
}
/**
! * This tests to make sure if putting money on the gift card fails that
! * we will return money back to the users credit card.
*
* @see SysUnderTest#buyGiftCard
***************
*** 94,97 ****
--- 132,136 ----
*/
public void testFailureOfBuyingGiftCard() {
+ //Now, we want to simulate a failure that should cause recovery to occur....
mockGiftSvc.addThrowException(new RuntimeException("cause failure in subsystem"), "putMoneyOnCard");
***************
*** 99,109 ****
double amount = 40.01;
try {
sysUnderTest.buyGiftCard(user, amount);
fail("Should have thrown exception");
} catch(PurchaseException e) {}
! //make sure takeMoney then return money was called which would happen
! //in a failure case. Also could have made takeMoney ignored instead so
! //we didn't test the takeMoney and just tested returnMoney.
CalledMethod[] methods = mockCreditSvc.expect("takeMoney", "returnMoney");
CalledMethod takeMoney = methods[0];
--- 138,148 ----
double amount = 40.01;
try {
+ //Now call the system which should recover
sysUnderTest.buyGiftCard(user, amount);
fail("Should have thrown exception");
} catch(PurchaseException e) {}
! //make sure takeMoney then return money was called which should happen
! //in our failure case.
CalledMethod[] methods = mockCreditSvc.expect("takeMoney", "returnMoney");
CalledMethod takeMoney = methods[0];
Index: CreditAuthorizationSvc.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/basic/CreditAuthorizationSvc.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** CreditAuthorizationSvc.java 10 Sep 2006 18:25:52 -0000 1.1
--- CreditAuthorizationSvc.java 11 Sep 2006 03:47:39 -0000 1.2
***************
*** 14,23 ****
public interface CreditAuthorizationSvc {
- /**
- * @param user
- * @param usDollars
- */
- public void authorize(String user, double usDollars);
-
public void takeMoney(String user, double usDollars);
public void returnMoney(String user, double usDollars);
--- 14,17 ----
Index: SysUnderTest.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/examples/basic/SysUnderTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** SysUnderTest.java 10 Sep 2006 18:25:52 -0000 1.1
--- SysUnderTest.java 11 Sep 2006 03:47:39 -0000 1.2
***************
*** 27,43 ****
* @showcode
*/
- public void purchase(String item, String user, double usDollars) {
- try {
- creditSvc.authorize(user, usDollars);
- } catch(RuntimeException e) {
- throw new PurchaseException(e);
- }
-
- //if authorize succeeded, go delete an item from the
- //Inventory as we successfully sold it.
- }
- /**
- * @showcode
- */
public void buyGiftCard(String user, double usDollars) {
creditSvc.takeMoney(user, usDollars);
--- 27,30 ----
|