KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > matcher > TestFailureMatcherTest


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

3 package test.jmock.core.matcher;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.matcher.TestFailureMatcher;
9 import test.jmock.core.testsupport.MethodFactory;
10
11
12 public class TestFailureMatcherTest
13         extends TestCase
14 {
15     static final String JavaDoc MESSAGE = "MESSAGE";
16
17     Invocation invocation;
18     TestFailureMatcher testFailureMatcher;
19
20     public void setUp() {
21         MethodFactory methodFactory = new MethodFactory();
22
23         invocation = new Invocation("INVOKED-OBJECT",
24                                     methodFactory.newMethod("ignoredName", MethodFactory.NO_ARGUMENTS, void.class,
25                                                             MethodFactory.NO_EXCEPTIONS),
26                                     new Object JavaDoc[0]);
27
28         testFailureMatcher = new TestFailureMatcher(MESSAGE);
29     }
30
31     public void testAlwaysMatches() {
32         assertTrue("matches", testFailureMatcher.matches(invocation));
33         invokeMatcher();
34         assertTrue("matches", testFailureMatcher.matches(invocation));
35     }
36
37     public void testAlwaysVerifies() throws Throwable JavaDoc {
38         testFailureMatcher.verify();
39         invokeMatcher();
40         testFailureMatcher.verify();
41     }
42
43     public void testThrowsAssertionFailedErrorWhenInvoked() throws Throwable JavaDoc {
44         try {
45             testFailureMatcher.invoked(invocation);
46         }
47         catch (AssertionFailedError ex) {
48             assertEquals("should be error message from stub",
49                          MESSAGE, ex.getMessage());
50             return;
51         }
52         fail("expected AssertionFailedError");
53     }
54
55     public void testUsesErrorMessageAsDescription() {
56         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
57
58         testFailureMatcher.describeTo(buffer);
59
60         assertEquals("description", MESSAGE, buffer.toString());
61     }
62
63     private void invokeMatcher() {
64         try {
65             testFailureMatcher.invoked(invocation);
66         }
67         catch (AssertionFailedError ex) {
68             // expected
69
}
70     }
71 }
72
Popular Tags