From: Tim M. <ma...@us...> - 2003-04-04 16:47:44
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic In directory sc8-pr-cvs1:/tmp/cvs-serv21824/src/core/test/mockobjects/dynamic Modified Files: Tag: DynamicMockExperiment MockTest.java Added Files: Tag: DynamicMockExperiment TestInterface.java TestThrowable.java SingleCallTest.java TestConstraint.java CallSetTest.java Removed Files: Tag: DynamicMockExperiment CallCollectionTest.java CallCounterTest.java Log Message: First stab at dynamic mocks with named calls etc. --- NEW FILE: TestInterface.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; /** * @author dev */ public interface TestInterface { public String aMethod( String arg1, String arg2 ) throws Throwable; public void aMethodWithNoArguments(); } --- NEW FILE: TestThrowable.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; /** * @author dev */ public class TestThrowable extends Throwable { public TestThrowable() { super(); } public TestThrowable(String message) { super(message); } public TestThrowable(String message, Throwable cause) { super(message, cause); } public TestThrowable(Throwable cause) { super(cause); } } --- NEW FILE: SingleCallTest.java --- /* * Created on 04-Apr-2003 * * To change this generated comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ package test.mockobjects.dynamic; import com.mockobjects.constraint.*; import com.mockobjects.dynamic.*; import com.mockobjects.util.*; import junit.framework.*; public class SingleCallTest extends TestCase { final String methodName = "methodName"; Mock mock = new Mock(TestInterface.class,"mock"); SingleCall call; public SingleCallTest(String name) { super(name); } public void testUncalledCallDoesNotVerify() { final String methodName = "methodName"; SingleCall call = new SingleCall(methodName, C.args() ); try { call.verify(); } catch( AssertionFailedError ex ) { AssertMo.assertIncludes("should include call name", methodName, ex.getMessage() ); return; } fail("expected verify to fail"); } public void testSuccessfulCallVerifies() throws Throwable { SingleCall call = new SingleCall("methodName", C.args() ); call.call( mock, "methodName", new Object[0] ); call.verify(); } public void testMultipleCallsFail() throws Throwable { SingleCall call = new SingleCall(methodName, C.args() ); call.call( mock, methodName, new Object[0] ); try { call.call( mock, methodName, new Object[0] ); } catch( AssertionFailedError ex ) { AssertMo.assertIncludes("should include call name", methodName, ex.getMessage() ); return; } fail("expected second call to fail"); } public void testArgumentsCheckedAgainstConstraints() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; TestConstraint[] constraints = { new TestConstraint( "constraint1", args[0], true ), new TestConstraint( "constraint2", args[1], true ), new TestConstraint( "constraint3", args[2], true ) }; SingleCall call = new SingleCall( methodName, (Constraint[])constraints ); call.call( mock, methodName, args ); for( int i = 0; i < constraints.length; i++ ) { constraints[i].verify(); } } public void testWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; TestConstraint[] constraints = { new TestConstraint( "constraint1", args[0], true ), new TestConstraint( "constraint2", args[1], true ), new TestConstraint( "constraint3", args[1], true ) }; SingleCall call = new SingleCall( methodName, (Constraint[])constraints ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should show expected number of arguments", Integer.toString(constraints.length), ex.getMessage()); AssertMo.assertIncludes("Should show actual number of arguments", Integer.toString(args.length), ex.getMessage()); AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); } public void testConstraintFailure() throws Throwable { String[] args = { "argA", "argB", "argC" }; TestConstraint[] constraints = { new TestConstraint( "constraintA", args[0], true ), new TestConstraint( "constraintB", args[1], false ), new TestConstraint( "constraintC", args[2], true ) }; SingleCall call = new SingleCall( methodName, (Constraint[])constraints ); boolean passed = true; try { call.call( mock, methodName, args ); passed = false; } catch (AssertionFailedError ex) { AssertMo.assertIncludes("Should include the method name", methodName, ex.getMessage()); AssertMo.assertIncludes("Should show index of failed argument", "1", ex.getMessage() ); AssertMo.assertIncludes("Should show expected arguments", AssertMo.join(constraints), ex.getMessage()); AssertMo.assertIncludes("Should show actual arguments", AssertMo.join(args), ex.getMessage()); } assertTrue("Should fail is call doesn't give an exception", passed); } public void testCallMatchesArguments() throws Throwable { String[] args = { "arg1", "arg2", "arg3" }; TestConstraint[] constraints = { new TestConstraint( "constraint1", args[0], true ), new TestConstraint( "constraint2", args[1], true ), new TestConstraint( "constraint3", args[2], true ) }; SingleCall call = new SingleCall( methodName, (Constraint[])constraints ); assertTrue( "call matches", call.matches( methodName, args) ); } public void testCallDoesNotMatchWhenWrongName() throws Throwable { SingleCall call = new SingleCall( methodName, new Constraint[0] ); assertFalse( "call does not match", call.matches( "anotherName", new Object[0]) ); } public void testCallDoesNotMatchWhenWrongNumberOfArguments() throws Throwable { String[] args = { "arg1", "arg2" }; TestConstraint[] constraints = { new TestConstraint( "constraint1", args[0], true ), new TestConstraint( "constraint2", args[1], true ), new TestConstraint( "constraint3", args[1], true ) }; SingleCall call = new SingleCall( methodName, (Constraint[])constraints ); assertFalse( "call does not match", call.matches( methodName, args) ); } public void testCallDoesNotMatchWhenConstraintIsViolated() throws Throwable { String[] args = { "argA", "argB", "argC" }; TestConstraint[] constraints = { new TestConstraint( "constraintA", args[0], true ), new TestConstraint( "constraintB", args[1], false ), new TestConstraint( "constraintC", args[2], true ) }; SingleCall call = new SingleCall( methodName, (Constraint[])constraints ); assertFalse( "call does not match", call.matches( methodName, args) ); } } --- NEW FILE: TestConstraint.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; import junit.framework.*; import com.mockobjects.*; import com.mockobjects.constraint.*; /** * @author dev */ public class TestConstraint extends Assert implements Constraint, Verifiable { private String description; private Object expectedArg; private boolean result; private boolean wasChecked = false; public TestConstraint( String description, Object expectedArg, boolean result ) { this.description = description; this.expectedArg = expectedArg; this.result = result; } public String toString() { return description; } public boolean eval( Object o ) { assertSame( "should be expected argument", expectedArg, o ); wasChecked = true; return result; } public void verify() { assertTrue( description + " should have been checked", wasChecked ); } } --- NEW FILE: CallSetTest.java --- /* * Created on 04-Apr-2003 */ package test.mockobjects.dynamic; import com.mockobjects.dynamic.*; import junit.framework.*; /** * @author dev */ public class CallSetTest extends TestCase { private CallSet callSet = new CallSet(); private Mock unusedMock = null; public CallSetTest(String name) { super(name); } public void testEmptySetVerifies() throws Exception { callSet.verify(); } public void testSingleItemNotCalledFails() throws Exception { callSet.add(new SingleCall("methodThatWontBeCalled",C.args())); try { callSet.verify(); } catch (AssertionFailedError ex) { return; } fail("Should have got a failure for a method that wasn't called"); } public void testSingleItemCalledSuccessfully() throws Throwable { callSet.add(new SingleCall("methodThatWillBeCalled",C.args())); callSet.call( unusedMock, "methodThatWillBeCalled", new Object[0] ); callSet.verify(); } public void testTwoItemsCalledSameOrder() throws Throwable { callSet.add(new SingleCall("methodA",C.args())); callSet.add(new SingleCall("methodB",C.args())); callSet.call( unusedMock, "methodA", new Object[0] ); callSet.call( unusedMock, "methodB", new Object[0] ); callSet.verify(); } } Index: MockTest.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic/MockTest.java,v retrieving revision 1.8 retrieving revision 1.8.2.1 diff -u -r1.8 -r1.8.2.1 --- MockTest.java 12 Nov 2002 17:42:38 -0000 1.8 +++ MockTest.java 4 Apr 2003 16:47:39 -0000 1.8.2.1 @@ -1,563 +1,222 @@ -/* Copyright (c) 2002 Nat Pryce. All rights reserved. - * - * Created on February 11, 2002, 12:34 AM +/* + * Created on 04-Apr-2003 + * + * To change this generated comment go to + * Window>Preferences>Java>Code Generation>Code and Comments */ package test.mockobjects.dynamic; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import junit.framework.AssertionFailedError; - import com.mockobjects.dynamic.*; -import com.mockobjects.constraint.*; -import com.mockobjects.util.TestCaseMo; +import com.mockobjects.util.*; + +import junit.framework.*; + +/** + * @author dev + * + * To change this generated comment go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +public class MockTest extends TestCase { + private Mock aMock; + private TestInterface proxy; + private static final String MOCK_NAME = "Test mock"; + + public MockTest(String name) { + super(name); + } + + public void setUp() { + aMock = new Mock( TestInterface.class, MOCK_NAME ); + try { + proxy = (TestInterface)aMock.proxy(); + } + catch( ClassCastException ex ) { + fail("proxy is not of expected interface type"); + } + } + + public void testNewMockVerifies() throws Exception { + aMock.verify(); + } + + public void testMockNameFromClass() throws Exception { + assertEquals( "mock-String", Mock.mockNameFromClass(String.class) ); + } + + public void testVerifyFailsIfExpectedCallNotCalled() throws Throwable { + aMock.expect( new CallMocker() { + public void verify() { throw new AssertionFailedError("example error"); } + public boolean matches(String methodName, Object[] args) { + return false; + } + public Object call(Mock mock, String methodName, Object[] args) { + return null; + } + } ); + + try { + aMock.verify(); + } + catch( AssertionFailedError ex ) { + AssertMo.assertIncludes( "mock name should be in exception message", + MOCK_NAME, ex.getMessage() ); + return; + } + fail("verify should have failed"); + } + + public void testVerifiesIfExpectedCallIsCalled() throws Throwable { + aMock.expect( new CallMocker() { + public void verify() {} + public boolean matches(String methodName, Object[] args) { + return false; + } + + public Object call(Mock mock, String methodName, Object[] args) { + return null; + } + } ); + + proxy.aMethod( "hello", "world" ); + aMock.verify(); + } + + public void testCallMockerReceivesArgumentsFromProxy() throws Throwable { + final String[] expectedArgs = { "hello", "world" }; + final String[] receivedArgs = new String[2]; + + aMock.expect( new CallMocker() { + public void verify() {} + public boolean matches(String methodName, Object[] args) { + return false; + } + + public Object call(Mock mock, String methodName, Object[] args) { + assertEquals( "should be expected number of args", expectedArgs.length, args.length ); + System.arraycopy( args, 0, receivedArgs, 0, args.length ); + return null; + } + } ); + + proxy.aMethod( expectedArgs[0], expectedArgs[1] ); + for( int i = 0; i < expectedArgs.length; i++ ) { + assertSame( "arg " + (i+1), expectedArgs[i], receivedArgs[i] ); + } + } + + public void testCallMockerReceivesEmptyArrayWhenNoArguments() throws Exception { + aMock.expect( new CallMocker() { + public void verify() {} + public boolean matches(String methodName, Object[] args) { + return false; + } + public Object call( Mock mock, String methodName, Object[] args ) { + assertNotNull( "Arg array should not be null", args ); + assertEquals( "Arg array should have zero length", 0, args.length ); + return null; + } + } ); + + proxy.aMethodWithNoArguments(); + } + + public void testCallMockerResultReturnedByProxy() throws Throwable { + final String result = "result"; + + aMock.expect( new CallMocker() { + public void verify() {} + + public Object call(Mock mock, String methodName, Object[] args) { + return result; + } + + public boolean matches(String methodName, Object[] args) { + return false; + } + } ); + + assertSame( "result is returned by mock", + result, proxy.aMethod( "hello", "world" ) ); + } + + public void testCallMockerThrowableThrownThroughProxy() throws Throwable { + final Throwable throwable = new TestThrowable(); + + aMock.expect( new CallMocker() { + public void verify() {} + public boolean matches(String methodName, Object[] args) { + return false; + } + + public Object call(Mock mock, String methodName, Object[] args) + throws Throwable + { + throw throwable; + } + } ); + + try { + proxy.aMethod( "hello", "world" ); + } + catch( Throwable ex ) { + assertSame( "exception is caught by mock", + throwable, ex ); + } + } + + public void testCallMockerAnnotatesAssertionFailedErrors() throws Throwable { + final String originalMessage = "original message"; + final AssertionFailedError error = new AssertionFailedError(originalMessage); + + aMock.expect( new CallMocker() { + public void verify() {} + public boolean matches(String methodName, Object[] args) { + return false; + } + + public Object call(Mock mock, String methodName, Object[] args) + throws Throwable + { + throw error; + } + } ); + + try { + proxy.aMethod( "hello", "world" ); + } + catch( AssertionFailedError err ) { + AssertMo.assertIncludes( "should contain original message", + originalMessage, err.getMessage() ); + AssertMo.assertIncludes( "should contain mock name", + MOCK_NAME, err.getMessage() ); + } + } + + public void testMockDispatchesCallToMatchingCallMocker() throws Throwable { + final String[] expectedArgs = { "hello", "world" }; + final String[] receivedArgs = new String[2]; + + aMock.expect( new CallMocker() { + public void verify() {} + public boolean matches(String methodName, Object[] args) { + return false; + } + + public Object call(Mock mock, String methodName, Object[] args) { + assertEquals( "should be expected number of args", expectedArgs.length, args.length ); + System.arraycopy( args, 0, receivedArgs, 0, args.length ); + return null; + } + } ); + + proxy.aMethod( expectedArgs[0], expectedArgs[1] ); + + for( int i = 0; i < expectedArgs.length; i++ ) { + assertSame( "arg " + (i+1), expectedArgs[i], receivedArgs[i] ); + } + } + -public class MockTest - extends TestCaseMo -{ - public static interface ExampleInterface { - Object noArgs(); - void voidMethod(); - String objectTypes( Integer i ); - int primitiveTypes( int x, int y ); - void sideEffect( StringBuffer buf ); - - // These are used to test default return values - boolean booleanResult(); - byte byteResult(); - char charResult(); - short shortResult(); - int intResult(); - long longResult(); - float floatResult(); - double doubleResult(); - String stringResult(); - - // Used to test ordered calls - void first(); - void second(); - } - - private Mock _mock; - private ExampleInterface _interface; - - - public MockTest( String test ) { - super(test); - } - - public void setUp() { - _mock = new Mock( ExampleInterface.class ); - _interface = (ExampleInterface)_mock.proxy(); - } - - public void tearDown() { - _mock = null; - _interface = null; - } - - public void testNewMockVerifies() { - _mock.verify(); - } - - public void testMockCallToNoArgMethod() { - Object result = new Object(); - - _mock.expectAndReturn( "noArgs", Mock.NO_ARGS, result ); - - assertSame( result, _interface.noArgs() ); - - _mock.verify(); - } - - public void testMockCallToVoidMethod() { - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - - _interface.voidMethod(); - - _mock.verify(); - } - - public void testObjectTypes() { - _mock.expectAndReturn( "objectTypes", - new Constraint[] { new IsEqual( new Integer(1) ) }, - "1" ); - - assertEquals( "1", _interface.objectTypes( new Integer(1) ) ); - _mock.verify(); - } - - public void testPrimitiveTypes() { - _mock.expectAndReturn( "primitiveTypes", - new Constraint[] { new IsEqual( new Integer(2) ), - new IsEqual( new Integer(3) ) }, - new Integer(6) ); - - assertEquals( 6, _interface.primitiveTypes(2,3) ); - _mock.verify(); - } - - public void testLaterExpectationsOverrideEarlierExpectations() { - Object result1 = new Object(); - Object result2 = new Object(); - - _mock.expectAndReturn( "noArgs", Mock.NO_ARGS, result1 ); - _mock.expectAndReturn( "noArgs", Mock.NO_ARGS, result2 ); - - assertSame( result2, _interface.noArgs() ); - assertSame( result2, _interface.noArgs() ); - _mock.verify(); - } - - public void testThrow() { - _mock.expectAndThrow( "primitiveTypes", - new Constraint[] { new IsEqual( new Integer(2) ), - new IsEqual( new Integer(3) ) }, - new ArithmeticException("message") ); - - try { - _interface.primitiveTypes( 2, 3 ); - fail( "expected ArithmeticException to be thrown" ); - } - catch( ArithmeticException ex ) { - assertEquals( "message", ex.getMessage() ); - } - - _mock.verify(); - } - - public void testExpectCallWithSideEffect() { - final StringBuffer buf = new StringBuffer(); - _mock.expect( "sideEffect", new MockCall() { - public Object call( Object[] args ) throws Throwable { - assertSame( "wrong buffer", buf, args[0] ); - buf.append("hello"); - return Mock.VOID; - } - } ); - - _interface.sideEffect( buf ); - assertEquals( "hello", buf.toString() ); - _mock.verify(); - } - - public void testFailingExpectationInCallWithSideEffect() { - final StringBuffer buf = new StringBuffer(); - _mock.expect( "sideEffect", new MockCall() { - public Object call( Object[] args ) throws Throwable { - assertSame( "wrong buffer", buf, args[0] ); - buf.append("hello"); - return Mock.VOID; - } - } ); - - assertFails( "should fail with wrong buffer", new Runnable() { - public void run() { - _interface.sideEffect( new StringBuffer() ); - } - } ); - } - - public void testNotAllMethodsCalled() { - _mock.expectVoid("noArgs", Mock.NO_ARGS); - - assertFails("verify did not fail when not all of the expected methods were called", - new Runnable() { - public void run() { _mock.verify(); } - }); - } - - public void testCalledExpectationClearedBySubsequentExpecation() { - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - _interface.voidMethod(); - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - - assertFails("verify did not fail when not all of the expected methods were called", - new Runnable() { - public void run() { _mock.verify(); } - }); - } - - public void testUnexpectedMethodThrowsWhenStrict() { - _mock.setStrict(true); - - assertFails("strict mock did not fail when unexpected method was called", - new Runnable() { - public void run() { _interface.voidMethod();} - }); - } - - public void testUnexpectedMethodDoesNotThrowWhenNotStrict() { - try { - _interface.voidMethod(); - } - catch( AssertionFailedError ex ) { - fail( "unstrict mock failed when unexpected method was called" ); - } - } - - public void testDefaultResultFromUnexpectedCalls() { - assertEquals( false, _interface.booleanResult() ); - assertEquals( 0, _interface.byteResult() ); - assertEquals( '\0', _interface.charResult() ); - assertEquals( 0, _interface.shortResult() ); - assertEquals( 0, _interface.intResult() ); - assertEquals( 0L, _interface.longResult() ); - assertEquals( 0.0f, _interface.floatResult(), 0.0f ); - assertEquals( 0.0d, _interface.doubleResult(), 0.0d ); - assertEquals( "", _interface.stringResult() ); - } - - public void testSetupDefaultResult() { - _mock.setupDefaultResult( boolean.class, new Boolean(true) ); - _mock.setupDefaultResult( byte.class, new Byte((byte)1) ); - _mock.setupDefaultResult( char.class, new Character( '2' ) ); - _mock.setupDefaultResult( short.class, new Short( (short)3 ) ); - _mock.setupDefaultResult( int.class, new Integer(4) ); - _mock.setupDefaultResult( long.class, new Long(5L) ); - _mock.setupDefaultResult( float.class, new Float(6.0f) ); - _mock.setupDefaultResult( double.class, new Double(7.0d) ); - _mock.setupDefaultResult( String.class, "8" ); - - assertEquals( true, _interface.booleanResult() ); - assertEquals( (byte)1, _interface.byteResult() ); - assertEquals( '2', _interface.charResult() ); - assertEquals( 3, _interface.shortResult() ); - assertEquals( 4, _interface.intResult() ); - assertEquals( 5L, _interface.longResult() ); - assertEquals( 6.0f, _interface.floatResult(), 0.0f ); - assertEquals( 7.0d, _interface.doubleResult(), 0.0d ); - assertEquals( "8", _interface.stringResult() ); - } - - public void testWrongNumberOfArguments() { - Constraint p = new IsEqual( new Integer(2) ); - Constraint q = new IsAnything(); - - _mock.expectAndReturn( "objectTypes", new Constraint[]{p,q}, "2" ); - - assertFails( "mock did not fail when wrong number of arguments passed", - new Runnable() { - public void run() { _interface.objectTypes( new Integer(1) ); } - }); - } - - public void testArgumentsPassedToNoArgMethod() { - Constraint p = new IsAnything(); - - _mock.expectVoid( "voidMethod", new Constraint[]{p} ); - - try { - _interface.voidMethod(); - } - catch( AssertionFailedError ex ) { - return; - } - - fail( "mock did not fail when arguments passed to void method" ); - } - - public void testPredicateFailure() { - Constraint p = new IsEqual( new Integer(2) ); - - _mock.expectAndReturn( "objectTypes", new Constraint[]{p}, "2" ); - - assertTrue( !p.eval( new Integer(1) ) ); - try { - _interface.objectTypes( new Integer(1) ); - } - catch( AssertionFailedError ex ) { - return; - } - - fail( "mock did not fail when predicate returned false" ); - } - - public void testErrorWhenOrderedMethodsCalledInWrongOrder() { - _mock.expectVoid( "first", Mock.NO_ARGS ); - _mock.expectVoid( "second", Mock.NO_ARGS ); - - _mock.order( "first", "second" ); - - try { - _interface.second(); - } - catch( AssertionFailedError ex ) { - return; - } - - fail( "mock did not enforce order of calls" ); - } - - public void testExpectNotCalled() { - _mock.expectNotCalled("noArgs"); - try { - _interface.noArgs(); - } - catch( AssertionFailedError ex ) { - return; - } - - fail("AssertionFailedError expected"); - } - - public void testExpectNotCalledClearedBySubsequentExpectation() { - _mock.expectNotCalled("voidMethod"); - _mock.expectVoid( "voidMethod", Mock.NO_ARGS ); - - _interface.voidMethod(); - - _mock.verify(); - } - - public void testSetupResult() { - final String RESULT = "result"; - - _mock.setupResult( "objectTypes", RESULT ); - - assertEquals( RESULT, _interface.objectTypes(null) ); - assertEquals( RESULT, _interface.objectTypes(new Integer(0)) ); - - _mock.verify(); - } - - public void testSetupThrow() { - _mock.setupThrow( "objectTypes", new IllegalArgumentException() ); - - try { - _interface.objectTypes(null); - fail("expected IllegalArgumentException"); - } - catch( IllegalArgumentException ex ) { - // expected - } - - try { - _interface.objectTypes(new Integer(0)); - fail("expected IllegalArgumentException"); - } - catch( IllegalArgumentException ex ) { - // expected - } - - _mock.verify(); - } - - public void testSetupCallWithSideEffect() { - final StringBuffer buf = new StringBuffer(); - _mock.setup( "sideEffect", new MockCall() { - public Object call( Object[] args ) throws Throwable { - buf.append("hello"); - return Mock.VOID; - } - } ); - - _interface.sideEffect( buf ); - assertEquals( "hello", buf.toString() ); - _mock.verify(); - } - - public void testSetupResultDoesNotRequireCall() { - _mock.setupResult( "objectTypes", "result" ); - _mock.verify(); - } - - public void testSetupThrowDoesNotRequireCall() { - _mock.setupThrow( "objectTypes", new RuntimeException() ); - _mock.verify(); - } - - public void testSetupCallWithSideEffectDoesNotRequireCall() { - _mock.setup( "sideEffect", new MockVoidCall() { - public Object call( Object[] args ) throws Throwable { - throw new RuntimeException("should not be called"); - } - } ); - - _mock.verify(); - } - - public void testNoErrorWhenOrderedMethodsCalledInCorrectOrder() { - _mock.expectVoid( "first", Mock.NO_ARGS ); - _mock.expectVoid( "second", Mock.NO_ARGS ); - - _mock.order( "first", "second" ); - - _interface.first(); - _interface.second(); - } - - public void testErrorWhenMockedVoidMethodReturnsAValue() { - _mock.expectAndReturn( "voidMethod", Mock.NO_ARGS, Boolean.TRUE ); - - try { - _interface.voidMethod(); - } - catch( AssertionFailedError ex ) { - return; - } - fail("should have thrown AssertionFailedError when return from void method"); - } - - public void testEquals() { - assertEquals("Same interface should have same hashcode", _interface.hashCode(), _interface.hashCode()); - assertEquals("Same interface should be equal", _interface, _interface); - } - - public static interface Interface1 { - void method1(); - } - - public static interface Interface2 { - void method2(); - } - - public static interface Interface3 { - void method3(); - } - - public static interface Interface4 { - void method4(); - } - - public static interface Interface5 { - void method5(); - } - - public void testMockTwoInterfaces() { - Mock mock = new Mock( Interface1.class, Interface2.class ); - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - } - - public void testMockThreeInterfaces() { - Mock mock = new Mock( Interface1.class, Interface2.class, - Interface3.class ); - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - assertInstanceOf( Interface3.class, mock.proxy() ); - } - - public void testMockFourInterfaces() { - Mock mock = new Mock( Interface1.class, Interface2.class, - Interface3.class, Interface4.class ); - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - assertInstanceOf( Interface3.class, mock.proxy() ); - assertInstanceOf( Interface4.class, mock.proxy() ); - } - - public void testMockFiveInterfaces() { - Mock mock = new Mock( new Class[] { - Interface1.class, Interface2.class, Interface3.class, - Interface4.class, Interface5.class } ); - - assertInstanceOf( Interface1.class, mock.proxy() ); - assertInstanceOf( Interface2.class, mock.proxy() ); - assertInstanceOf( Interface3.class, mock.proxy() ); - assertInstanceOf( Interface4.class, mock.proxy() ); - assertInstanceOf( Interface5.class, mock.proxy() ); - } - - private static void assertInstanceOf( Class klass, Object o ) { - assertTrue( o + " should be an instance of " + klass, - klass.isInstance(o) ); - } - - public void testFailureWhenExpectMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad method name", new Runnable() { - public void run() { - mock.expectVoid( "CeciNestPasUneMethode", Mock.NO_ARGS ); - } - } ); - } - - public void testFailureWhenSetupMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad method name", new Runnable() { - public void run() { - mock.setupResult( "CeciNestPasUneMethode", new Object() ); - } - } ); - } - - public void testFailureWhenExpectNotCalledMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad method name", new Runnable() { - public void run() { - mock.expectNotCalled( "CeciNestPasUneMethode" ); - } - } ); - } - - public void testFailureWhenOrderMethodNotOnMockedInterfaces() { - final Mock mock = new Mock( Interface1.class, Interface2.class ); - - assertFails( "should fail with bad first method name", new Runnable() { - public void run() { - mock.order( "CeciNestPasUneMethode", "method2" ); - } - } ); - assertFails( "should fail with bad second method name", new Runnable() { - public void run() { - mock.order( "method1", "CeciNestPasUneMethode" ); - } - } ); - } - - - - public static class DerivedMock - extends Mock - { - public boolean was_called = false; - - public DerivedMock() { - super( ExampleInterface.class ); - } - - public void voidMethod() { - this.was_called = true; - } - - public String objectTypes( Integer n ) throws Throwable { - Method method = getClass().getMethod("objectTypes", new Class[] { Integer.class } ); - return (String)mockCall(method , new Object[]{n} ); - } - }; - - public void testDerivedMockClass() { - DerivedMock mock = new DerivedMock(); - ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance( - getClass().getClassLoader(), - new Class[]{ ExampleInterface.class }, - mock ); - - i.voidMethod(); - - assertTrue( "mock method not called on derived class", - mock.was_called ); - mock.verify(); - } - - public void testDerivedClassPassingInvocationToMockCall() { - DerivedMock mock = new DerivedMock(); - ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance( - getClass().getClassLoader(), - new Class[]{ ExampleInterface.class }, - mock ); - - mock.expectAndReturn( "objectTypes", - new Constraint[] { new IsAnything() }, - "result" ); - - assertEquals( "result", i.objectTypes( new Integer(1) ) ); - - mock.verify(); - } } --- CallCollectionTest.java DELETED --- --- CallCounterTest.java DELETED --- |