KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > examples > calcserver > TestSavingCalculator


1 package com.mockobjects.examples.calcserver;
2
3 import java.sql.SQLException JavaDoc;
4 import com.mockobjects.sql.MockPreparedStatement;
5 import com.mockobjects.util.TestCaseMo;
6
7 public class TestSavingCalculator extends TestCaseMo {
8     private static final Class JavaDoc THIS = TestSavingCalculator.class;
9
10
11     public TestSavingCalculator(String JavaDoc name) {
12         super(name);
13     }
14
15
16     public static void main(String JavaDoc[] args) {
17         start(new String JavaDoc[]{ THIS.getName()});
18     }
19
20
21     public void testSave() throws CalculatorException {
22         MockCalculator myMockCalculator = new MockCalculator();
23         MockPreparedStatement myMockStatement = new MockPreparedStatement();
24
25         SqlSavingCalculator mySavingCalculator =
26                 new SqlSavingCalculator(myMockCalculator, myMockStatement);
27         myMockCalculator.setupResult(666);
28
29         myMockCalculator.setExpectedCalculation(5, 3, "/add");
30         myMockStatement.addExpectedSetParameter(1, 666);
31         myMockStatement.setExpectedExecuteCalls(1);
32
33         mySavingCalculator.calculate(5, 3, "/add");
34
35         myMockCalculator.verify();
36         myMockStatement.verify();
37     }
38
39
40     public void testSqlFailure() throws CalculatorException {
41         MockCalculator myMockCalculator = new MockCalculator();
42         MockPreparedStatement myMockStatement = new MockPreparedStatement();
43
44         SqlSavingCalculator mySavingCalculator =
45                 new SqlSavingCalculator(myMockCalculator, myMockStatement);
46
47         myMockCalculator.setExpectedCalculation(5, 3, "/add");
48         myMockStatement.setExpectedExecuteCalls(1);
49         myMockStatement.setupThrowExceptionOnExecute(new SQLException JavaDoc("Mock exception"));
50
51         try {
52             mySavingCalculator.calculate(5, 3, "/add");
53             fail("Should throw exception");
54         } catch (CalculatorException expected) {
55             //
56
}
57
58         myMockCalculator.verify();
59         myMockStatement.verify();
60     }
61 }
Popular Tags