|
From: Steve F. <sm...@us...> - 2002-10-25 22:07:50
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/test/mockobjects/dynamic
In directory usw-pr-cvs1:/tmp/cvs-serv31545/src/core/test/mockobjects/dynamic
Added Files:
MockTest.java
Removed Files:
Test_Mock.java
Log Message:
renamed Test_Mock to MockTest
refactored some tests to use assertFails()
--- NEW FILE: MockTest.java ---
/* Copyright (c) 2002 Nat Pryce. All rights reserved.
*
* Created on February 11, 2002, 12:34 AM
*/
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.util.TestCaseMo;
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();
}
public static class DerivedMock
extends Mock
{
DerivedMock( String name ) {
super(name);
}
public boolean was_called = false;
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} );
}
};
private Mock _mock;
private ExampleInterface _interface;
public MockTest( String test ) {
super(test);
}
public void setUp() {
_mock = new Mock();
_interface =
(ExampleInterface)_mock.createInterface( ExampleInterface.class );
}
public void tearDown() {
_mock = null;
_interface = null;
}
public void testNewMockVerifies() {
_mock.verify();
}
public void testNamedMock() {
Mock mock = new Mock("Name");
assertEquals( "Name", mock.toString() );
}
public void testMockCallToNoArgMethod() {
Object result = new Object();
_mock.expectReturn( "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.expectReturn( "objectTypes",
new Predicate[] { new IsEqual( new Integer(1) ) },
"1" );
assertEquals( "1", _interface.objectTypes( new Integer(1) ) );
_mock.verify();
}
public void testPrimitiveTypes() {
_mock.expectReturn( "primitiveTypes",
new Predicate[] { 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.expectReturn( "noArgs", Mock.NO_ARGS, result1 );
_mock.expectReturn( "noArgs", Mock.NO_ARGS, result2 );
assertSame( result2, _interface.noArgs() );
assertSame( result2, _interface.noArgs() );
_mock.verify();
}
public void testThrow() {
_mock.expectThrow( "primitiveTypes",
new Predicate[] { 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(
new ExpectedCall( "sideEffect",
new Predicate[]{ new IsSame(buf) } )
{
public Object eval( Object[] args ) throws Throwable {
buf.append("hello");
return Mock.VOID;
}
} );
_interface.sideEffect( buf );
assertEquals( "hello", buf.toString() );
_mock.verify();
}
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( "noArgs", Mock.NO_ARGS );
_interface.noArgs();
_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 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() {
Predicate p = new IsEqual( new Integer(2) );
Predicate q = new IsAnything();
_mock.expectReturn( "objectTypes", new Predicate[]{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() {
Predicate p = new IsAnything();
_mock.expectVoid( "voidMethod", new Predicate[]{p} );
try {
_interface.voidMethod();
}
catch( AssertionFailedError ex ) {
return;
}
fail( "mock did not fail when arguments passed to void method" );
}
public void testPredicateFailure() {
Predicate p = new IsEqual( new Integer(2) );
_mock.expectReturn( "objectTypes", new Predicate[]{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("noArgs");
_mock.expectVoid( "noArgs", Mock.NO_ARGS );
_interface.noArgs();
_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( new ExpectedCall("sideEffect") {
public Object eval( 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( new ExpectedCall("sideEffect") {
public Object eval( 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.expectReturn( "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 testDerivedMockClass() {
DerivedMock mock = new DerivedMock("Derived");
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("Derived");
ExampleInterface i = (ExampleInterface)Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[]{ ExampleInterface.class },
mock );
mock.expectReturn( "objectTypes",
new Predicate[] { new IsAnything() },
"result" );
assertEquals( "result", i.objectTypes( new Integer(1) ) );
mock.verify();
}
}
--- Test_Mock.java DELETED ---
|