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