invokedaftermatchertest.java

来自「不管是测试驱动开发或者是其它的开发模式」· Java 代码 · 共 84 行

JAVA
84
字号
/*  Copyright (c) 2000-2004 jMock.org
 */
package test.jmock.core.matcher;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.jmock.core.Invocation;
import org.jmock.core.matcher.InvokedAfterMatcher;
import org.jmock.core.matcher.InvokedRecorder;
import test.jmock.core.testsupport.MethodFactory;


public class InvokedAfterMatcherTest extends TestCase
{
    private static final String PRIOR_CALL_ID = "RECORDER-ID";

    private Invocation invocation1;
    private Invocation invocation2;
    private InvokedRecorder recorder;
    private InvokedAfterMatcher after;


    public void setUp() {
        MethodFactory methodFactory = new MethodFactory();

        invocation1 = new Invocation("INVOKED-OBJECT-1",
                                     methodFactory.newMethod("invocation1", MethodFactory.NO_ARGUMENTS,
                                                             Void.class, MethodFactory.NO_EXCEPTIONS),
                                     new Object[0]);

        invocation2 = new Invocation("INVOKED-OBJECT-2",
                                     methodFactory.newMethod("invocation2", MethodFactory.NO_ARGUMENTS,
                                                             Void.class, MethodFactory.NO_EXCEPTIONS),
                                     new Object[0]);

        recorder = new InvokedRecorder();
        after = new InvokedAfterMatcher(recorder, PRIOR_CALL_ID);
    }

    public void testOnlyMatchesWhenInvokedAfterPriorInvocation() {
        assertFalse("should not match before previous invocation",
                    after.matches(invocation2));
        recorder.invoked(invocation1);
        assertTrue("should match after previous invocation",
                   after.matches(invocation2));
    }

    // Note: this will not happen in typical usage because it will not match out of order
    public void testFailsIfInvokedOutOfOrder() {
        try {
            after.invoked(invocation2);
        }
        catch (AssertionFailedError ex) {
            assertTrue("should contain description of prior call",
                       ex.getMessage().indexOf(PRIOR_CALL_ID) >= 0);
            return;
        }
        fail("AssertionFailedError expected");
    }

    public void testIdentifiesPriorCallInDescription() {
        StringBuffer buf = new StringBuffer();

        after.describeTo(buf);

        assertTrue("should include 'after <id of prior call>' in description",
                   buf.toString().indexOf("after " + PRIOR_CALL_ID) >= 0);
    }

    public void testIncludesWhetherPriorCallHasOccurredInDescription() {
        String description;

        description = after.describeTo(new StringBuffer()).toString();
        assertTrue( "should include \"(not invoked)\"",
                    description.indexOf("(not invoked)") >= 0 );

        recorder.invoked(invocation1);
        description = after.describeTo(new StringBuffer()).toString();
        assertTrue( "should include \"(invoked)\"",
                    description.indexOf("(invoked)") >= 0 );

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?