KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > examples > calculator > expression > DivisionTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.examples.calculator.expression;
4
5
6 import org.jmock.examples.calculator.CalculatorException;
7 import org.jmock.examples.calculator.Expression;
8 import org.jmock.examples.calculator.SimpleEnvironment;
9 import org.jmock.examples.calculator.expression.Division;
10
11
12 public class DivisionTest extends AbstractBinaryOperatorTest
13 {
14
15     public void testDividesLeftSubexpressionByRightSubexpression() throws Exception JavaDoc {
16         runOperatorTest();
17     }
18
19     public void testThrowsExceptionOnDivideByZero() throws Exception JavaDoc {
20         Expression expression = makeExpression(1, 0);
21
22         try {
23             expression.evaluate(new SimpleEnvironment());
24             fail("expected CalculatorException on divide by zero");
25         }
26         catch (CalculatorException ex) {
27             assertTrue("error should contain 'divide by zero'",
28                        ex.getMessage().indexOf("divide by zero") >= 0);
29         }
30     }
31
32     protected double expectedValue( double left, double right ) {
33         return left / right;
34     }
35
36     protected Expression makeExpression( Expression left, Expression right ) {
37         return new Division(left, right);
38     }
39 }
40
Popular Tags