1 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.InvokeOnceMatcher; 9 import org.jmock.expectation.AssertMo; 10 import test.jmock.core.testsupport.MethodFactory; 11 12 13 public class InvokeOnceMatcherTest extends TestCase 14 { 15 private Invocation emptyInvocation; 16 private InvokeOnceMatcher matcher; 17 18 public void setUp() { 19 MethodFactory methodFactory = new MethodFactory(); 20 emptyInvocation = new Invocation("INVOKED-OBJECT", 21 methodFactory.newMethod("example", MethodFactory.NO_ARGUMENTS, Void .class, MethodFactory.NO_EXCEPTIONS), 22 new Object [0]); 23 matcher = new InvokeOnceMatcher(); 24 } 25 26 public void testWillMatchIfNotYetInvoked() { 27 assertTrue("Should match", matcher.matches(emptyInvocation)); 28 } 29 30 public void testVerifyFailsIfNotYetInvoked() { 31 try { 32 matcher.verify(); 33 } 34 catch (AssertionFailedError ex) { 35 AssertMo.assertIncludes("should report expected method not invoked", 36 "expected method was not invoked", ex.getMessage()); 37 return; 38 } 39 fail("Should have thrown exception"); 40 } 41 42 public void testWillNotMatchAfterInvocation() { 43 matcher.invoked(emptyInvocation); 44 assertFalse("Should not match", matcher.matches(emptyInvocation)); 45 } 46 47 public void testVerifyPassesAfterInvocation() { 48 matcher.invoked(emptyInvocation); 49 matcher.verify(); 50 } 51 52 private static final String MATCH_DESCRIPTION = "expected once"; 53 54 public void testWritesDescriptionOfMatch() { 55 String description = matcher.describeTo(new StringBuffer ()).toString(); 56 57 assertTrue("should have description", matcher.hasDescription()); 58 assertEquals("should describe match", MATCH_DESCRIPTION, description); 59 } 60 61 public void testReportsWhetherCalledInDescription() { 62 matcher.invoked(emptyInvocation); 63 64 String description = matcher.describeTo(new StringBuffer ()).toString(); 65 66 assertTrue("should have description", matcher.hasDescription()); 67 assertTrue("should describe match", 68 description.indexOf(MATCH_DESCRIPTION) >= 0); 69 assertTrue("should report has been called", 70 description.indexOf("has been invoked") >= 0); 71 } 72 } 73 | Popular Tags |