Package org.jmock.dynamic

Source Code of org.jmock.dynamic.InvocationMockerTest

/* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
package org.jmock.dynamic;

import org.jmock.*;
import org.jmock.expectation.ExpectationCounter;
import org.jmock.expectation.ExpectationValue;
import org.jmock.expectation.Verifier;
import org.jmock.matcher.ArgumentsMatcher;
import org.jmock.matcher.MethodNameMatcher;
import org.jmock.matcher.StatelessInvocationMatcher;
import org.jmock.stub.VoidStub;

public class InvocationMockerTest extends AbstractTestCase {
    private InvocationMatcher matchAll = new StatelessInvocationMatcher() {
        public boolean matches(Invocation invocation) {
            return true;
        }
    };
    private InvocationMatcher matchNone = new StatelessInvocationMatcher() {
        public boolean matches(Invocation invocation) {
            return false;
        }
    };

    public class MockInvocationMatcher implements InvocationMatcher {
        public ExpectationValue invocation = new ExpectationValue("MockInvocationMatcher.invoked");
        public ExpectationValue match = new ExpectationValue("MockInvocationMatcher.matches");
        public ExpectationCounter verifyCalls = new ExpectationCounter("Verify calls");

        public boolean matches(Invocation invocation) {
            match.setActual(invocation);
            return true;
        }

        public void invoked(Invocation invocation) {
            this.invocation.setActual(invocation);
        }

        public void verify() {
            verifyCalls.inc();
        }
    }

    public class MockStub implements Stub {
        public ExpectationValue stubInvocation = new ExpectationValue("Stub invocation");

        public Object invoke(Invocation invocation) throws Throwable {
            stubInvocation.setActual(invocation);
            return "stub result";
        }

        public String getDescription() {
            return "MockStub";
        }
    };

    private Invocation exampleInvocation =
            new Invocation("example", new Class[]{String.class, String.class}, Void.class,
                    new Object[]{"arg1", "arg2"});


    public void testMatchesIfEverythingMatches() {
        InvocationMocker invocationMocker =
                new InvocationMocker(new InvocationMatcher[]{matchAll, matchAll}, null);

        assertTrue("Should have matched", invocationMocker.matches(exampleInvocation));
    }

    public void testDoesNotMatchIfEverythingMatches() {
        InvocationMocker invocationMocker =
                new InvocationMocker(new InvocationMatcher[]{matchAll, matchNone}, null);

        assertFalse("Should not have matched", invocationMocker.matches(exampleInvocation));
    }

    public void testMatchesInvocationWithParameters() {
        InvocationMocker invocationMocker = new InvocationMocker(
                new InvocationMatcher[]{
                    new MethodNameMatcher("example"),
                    new ArgumentsMatcher(new Constraint[]{C.eq("arg1"), C.eq("arg2")})}, null);

        assertTrue("Should have matched", invocationMocker.matches(exampleInvocation));
    }

    public void testDoesNotMatchWithDifferentParameter() {
        InvocationMocker invocationMocker = new InvocationMocker(
                new InvocationMatcher[]{
                    new MethodNameMatcher("example"),
                    new ArgumentsMatcher(new Constraint[]{C.eq("arg1"), C.eq("not arg2")})}, null);

        assertFalse("Should not have matched", invocationMocker.matches(exampleInvocation));
    }

    public void testMatchesInvocationBeforeCallingStub() throws Throwable {
        MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher();

        InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[]{mockInvocationMatcher}, new VoidStub());
        mockInvocationMatcher.invocation.setExpected(exampleInvocation);

        mocker.invoke(exampleInvocation);

        Verifier.verifyObject(mockInvocationMatcher);
    }

    public void testDelegatesVerifyToInvocationMatchers() throws Throwable {
        MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher();

        InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[]{mockInvocationMatcher}, new VoidStub());
        mockInvocationMatcher.verifyCalls.setExpected(1);

        mocker.verify();

        Verifier.verifyObject(mockInvocationMatcher);
    }


    public void testDelegatesInvocationToStubObject() throws Throwable {
        MockStub mockStub = new MockStub();

        InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[0], mockStub);

        mockStub.stubInvocation.setExpected(exampleInvocation);

        assertEquals("Should be invoke result", "stub result", mocker.invoke(exampleInvocation));

        Verifier.verifyObject(mockStub);
    }

    public void testCanAddExtraMatchers() throws Throwable {
        MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher();

        InvocationMocker mocker = new InvocationMocker(new InvocationMatcher[0], new VoidStub());
        mockInvocationMatcher.match.setExpected(exampleInvocation);
        mockInvocationMatcher.invocation.setExpected(exampleInvocation);

        mocker.addMatcher(mockInvocationMatcher);
        mocker.matches(exampleInvocation);
        mocker.invoke(exampleInvocation);

        Verifier.verifyObject(mockInvocationMatcher);
    }
}
TOP

Related Classes of org.jmock.dynamic.InvocationMockerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');