From: Steve F. <sm...@us...> - 2002-10-25 20:59:46
|
Update of /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic In directory usw-pr-cvs1:/tmp/cvs-serv2214/src/core/com/mockobjects/dynamic Modified Files: Mock.java Log Message: added check against returning values from void methods removed duplication in overloaded expect methods tidied up some formatting Index: Mock.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/core/com/mockobjects/dynamic/Mock.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Mock.java 21 Oct 2002 22:51:26 -0000 1.1 +++ Mock.java 25 Oct 2002 20:59:41 -0000 1.2 @@ -4,33 +4,26 @@ */ package com.mockobjects.dynamic; +import com.mockobjects.Verifiable; +import junit.framework.Assert; import junit.framework.AssertionFailedError; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.HashMap; -import java.util.Set; - -import com.mockobjects.dynamic.ExpectedCall; -import com.mockobjects.dynamic.ExpectedReturn; -import com.mockobjects.dynamic.ExpectedThrow; -import com.mockobjects.Verifiable; +import java.util.*; /** A convenient class for creating simple * <a href="http://www.mockobjects.com">mock objects</a>. */ public class Mock - extends junit.framework.Assert + extends Assert implements InvocationHandler, Verifiable { - /** A convenient constant for defining expectations for methods that + public static final Object VOID = new Object(); + + /** A convenient constant for defining expectations for methods that * have no methods. */ public static final Predicate[] NO_ARGS = new Predicate[0]; @@ -187,7 +180,7 @@ * before being returned to the caller of the method. */ public void expectReturn( String method, Object arg, Object result ) { - expect( new ExpectedReturn( method, P.arg(P.same(arg)), result ) ); + expectReturn(method, P.args(P.eq(arg)), result); } /** Expect a method call with no parameters and return a result when it is called. @@ -200,7 +193,7 @@ * before being returned to the caller of the method. */ public void expectReturn( String method, Object result ) { - expect( new ExpectedReturn( method, null, result ) ); + expectReturn(method, NO_ARGS, result ); } /** Expect a call to a method with a void return type. @@ -213,7 +206,7 @@ * the expected arity of the method call. */ public void expectVoid( String method, Predicate[] args ) { - expect( new ExpectedReturn( method, args, null ) ); + expect( new ExpectedReturn( method, args, VOID ) ); } /** Expect a call to a method with a void return type. @@ -224,7 +217,7 @@ * An single object that will be compared with predicate same() */ public void expectVoid(String method, Object arg) { - expect(new ExpectedReturn(method, P.arg(P.same(arg)), null)); + expectVoid(method, P.args(P.eq(arg))); } /** Expect a call to a method with a void return type and no parameters @@ -233,7 +226,7 @@ * The name of the method that will be called. */ public void expectVoid(String method) { - expect(new ExpectedReturn(method, null, null)); + expectVoid(method, NO_ARGS); } /** Expect a method call and throw an exception or error when it is called. @@ -247,9 +240,7 @@ * @param exception * The exception or error that will be thrown as a result of this call. */ - public void expectThrow( String method, Predicate[] args, - Throwable exception ) - { + public void expectThrow( String method, Predicate[] args, Throwable exception ) { expect( new ExpectedThrow( method, args, exception ) ); } @@ -262,9 +253,8 @@ * @param exception * The exception or error that will be thrown as a result of this call. */ - public void expectThrow( String method, Object arg, Throwable exception ) - { - expect( new ExpectedThrow( method, P.arg(P.same(arg)), exception ) ); + public void expectThrow( String method, Object arg, Throwable exception ) { + expectThrow( method, P.args(P.eq(arg)), exception ); } /** Expect a method call with no parameters and throw an exception or error when it is called. @@ -274,9 +264,8 @@ * @param exception * The exception or error that will be thrown as a result of this call. */ - public void expectThrow( String method, Throwable exception ) - { - expect( new ExpectedThrow( method, null, exception ) ); + public void expectThrow( String method, Throwable exception ) { + expectThrow( method, NO_ARGS, exception ); } /** Expect a method not to be called. @@ -317,8 +306,7 @@ try { return getClass().getMethod( - method.getName(), method.getParameterTypes() ).invoke( this, - args ); + method.getName(), method.getParameterTypes() ).invoke( this, args ); } catch( NoSuchMethodException ex ) { return mockCall( method, args ); @@ -339,8 +327,9 @@ if( expected.isTestingArguments() ) { checkArguments( expected, args ); } - return expected.eval( args ); - + + return checkResult(method, expected.eval( args )); + } else if( _strict ) { throw new AssertionFailedError( _name + ": unexpected call to " + method_name ); @@ -348,7 +337,15 @@ return defaultResult( method.getReturnType() ); } } - + + private Object checkResult(Method method, Object result) { + if( method.getReturnType() == void.class && VOID != result ) { + fail("trying to return " + result + " from void method"); + } + + return result; + } + private void checkCallOrder( String method_name ) { if( _order_constraints.containsKey(method_name) ) { assertMethodsHaveBeenCalled( |