private static <T> T doCreateMock(Class<T> type, ConstructorArgs constructorArgs, final IMocksControl control, Method... methods) { T mock; MocksControl mocksControl = ((MocksControl) control); if (constructorArgs == null) { if (methods == null) { mock = mocksControl.createMock(type); } else { mock = mocksControl.createMock(type, methods); } } else { if (methods == null) { mock = mocksControl.createMock(type, constructorArgs); } else { mock = mocksControl.createMock(type, constructorArgs, methods); } } return mock; }
@SuppressWarnings("deprecation") @Before public void setUp() throws Exception { businessMessagesMock = EasyMock.createMock(BusinessMessages.class); personServiceMock = EasyMock.createMock(PersonService.class); eventServiceMock = EasyMock.createMock(EventService.class); constructorArgs = new ConstructorArgs(SampleServiceWithoutPowerMockImpl.class.getConstructor(PersonService.class, EventService.class), personServiceMock, eventServiceMock); tested = createMock(SampleServiceWithoutPowerMockImpl.class, constructorArgs); }
@SuppressWarnings("deprecation") @Ignore("The initialize method is never invoked but is caught by the proxy. This is a possibly a bug in EasyMock class extensions?") @Test public void testPartialMock() throws Exception { /* * In the original test case PartialMockingWithConstructor had * constructor arguments which I removed to slim down the test case, * originally I was using the following method to create a partial mock. * Regardless the same problem still occurs. */ ConstructorArgs args = new ConstructorArgs(PartialMockingWithConstructor.class.getConstructor()); Method touchMethod = PartialMockingWithConstructor.class.getMethod("touch"); PartialMockingWithConstructor nationPartialMock = createMock(PartialMockingWithConstructor.class, args, touchMethod); /* * The following method also causes the same problem. */ // Nation nationPartialMock = // createPartialMockAndInvokeDefaultConstructor(Nation.class,"touch"); replay(nationPartialMock); // Uncommenting the following line has no effect on the test result. // nationPartialMock.initialize(); verify(nationPartialMock); }
@SuppressWarnings("deprecation") @Before public void setUp() throws Exception { businessMessagesMock = createMock(BusinessMessages.class); personServiceMock = createMock(PersonService.class); eventServiceMock = createMock(EventService.class); constructorArgs = new ConstructorArgs(SampleServiceWithoutPowerMockImpl.class.getConstructor(PersonService.class, EventService.class), personServiceMock, eventServiceMock); tested = createMock(SampleServiceWithoutPowerMockImpl.class, constructorArgs, new Method[] {}); }
static <T> T doMockSpecific(Class<T> type, MockStrategy mockStrategy, String[] methodNamesToMock, ConstructorArgs constructorArgs, Class<?>... argumentTypes) { List<Method> methods = new LinkedList<Method>(); for (String methodName : methodNamesToMock) { methods.add(WhiteboxImpl.findMethodOrThrowException(type, methodName, argumentTypes)); } final Method[] methodArray = methods.toArray(new Method[0]); if (WhiteboxImpl.areAllMethodsStatic(methodArray)) { if (mockStrategy instanceof DefaultMockStrategy) { mockStatic(type, methodArray); } else if (mockStrategy instanceof StrictMockStrategy) { mockStaticStrict(type, methodArray); } else { mockStaticNice(type, methodArray); } return null; } T mock = null; if (mockStrategy instanceof DefaultMockStrategy) { mock = createMock(type, constructorArgs, methodArray); } else if (mockStrategy instanceof StrictMockStrategy) { mock = createStrictMock(type, constructorArgs, methodArray); } else { mock = createNiceMock(type, constructorArgs, methodArray); } return mock; }
@Test public void testGetTheSecret_defaultConstructor() throws Exception { final Constructor<ConstructorArgsDemo> constructor = ConstructorArgsDemo.class.getConstructor((Class<?>[]) null); ConstructorArgsDemo tested = createMock(ConstructorArgsDemo.class, new ConstructorArgs(constructor)); assertEquals("default", Whitebox.getInternalState(tested, "secret", ConstructorArgsDemo.class)); }
/** * A utility method that may be used to mock several methods in an easy way * (by just passing in the method names of the method you wish to mock). Use * this to handle overloaded methods. The mock object created will support * mocking of final and native methods and invokes a specific constructor * based on the supplied argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodName * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param methodParameterTypes * Parameter types that defines the method. Note that this is * only needed to separate overloaded methods. * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. (optional) * @return the mock object. */ public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMockSpecific(type, new DefaultMockStrategy(), new String[] { methodName }, constructorArgs, methodParameterTypes); }
/** * A utility method that may be used to strictly mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). Use this to handle overloaded methods. The mock object created * will support mocking of final and native methods and invokes a specific * constructor based on the supplied argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodName * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param methodParameterTypes * Parameter types that defines the method. Note that this is * only needed to separate overloaded methods. * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. (optional) * @return the mock object. */ public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMockSpecific(type, new StrictMockStrategy(), new String[] { methodName }, constructorArgs, methodParameterTypes); }
/** * A utility method that may be used to nicely mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). Use this to handle overloaded methods. The mock object created * will support mocking of final and native methods and invokes a specific * constructor based on the supplied argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodName * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param methodParameterTypes * Parameter types that defines the method. Note that this is * only needed to separate overloaded methods. * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. (optional) * @return the mock object. */ public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMockSpecific(type, new NiceMockStrategy(), new String[] { methodName }, constructorArgs, methodParameterTypes); }
/** * A utility method that may be used to mock several methods in an easy way * (by just passing in the method names of the method you wish to mock). Use * this to handle overloaded methods <i>and</i> overloaded constructors. The * mock object created will support mocking of final and native methods and * invokes a specific constructor based on the supplied argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodName * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param methodParameterTypes * Parameter types that defines the method. Note that this is * only needed to separate overloaded methods. * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. * @param constructorParameterTypes * Parameter types that defines the constructor that should be * invoked. Note that this is only needed to separate overloaded * constructors. * @return the mock object. */ public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) { ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes), constructorArguments); return doMockSpecific(type, new DefaultMockStrategy(), new String[] { methodName }, constructorArgs, methodParameterTypes); }
/** * A utility method that may be used to strictly mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). Use this to handle overloaded methods <i>and</i> overloaded * constructors. The mock object created will support mocking of final and * native methods and invokes a specific constructor based on the supplied * argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodName * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param methodParameterTypes * Parameter types that defines the method. Note that this is * only needed to separate overloaded methods. * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. * @param constructorParameterTypes * Parameter types that defines the constructor that should be * invoked. Note that this is only needed to separate overloaded * constructors. * @return the mock object. */ public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) { ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes), constructorArguments); return doMockSpecific(type, new StrictMockStrategy(), new String[] { methodName }, constructorArgs, methodParameterTypes); }
/** * A utility method that may be used to nicely mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). Use this to handle overloaded methods <i>and</i> overloaded * constructors. The mock object created will support mocking of final and * native methods and invokes a specific constructor based on the supplied * argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodName * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param methodParameterTypes * Parameter types that defines the method. Note that this is * only needed to separate overloaded methods. * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. * @param constructorParameterTypes * Parameter types that defines the constructor that should be * invoked. Note that this is only needed to separate overloaded * constructors. * @return the mock object. */ public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes, Object[] constructorArguments, Class<?>[] constructorParameterTypes) { ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes), constructorArguments); return doMockSpecific(type, new NiceMockStrategy(), new String[] { methodName }, constructorArgs, methodParameterTypes); }
/** * Creates a mock object that supports mocking of final and native methods * and invokes a specific constructor. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArgs * The constructor arguments that will be used to invoke a * special constructor. * @param methods * optionally what methods to mock * @return the mock object. */ public static <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) { return doMock(type, false, new DefaultMockStrategy(), constructorArgs, methods); }
/** * Creates a mock object that supports mocking of final and native methods * and invokes a specific constructor based on the supplied argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. * @return the mock object. */ public static <T> T createMock(Class<T> type, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new DefaultMockStrategy(), constructorArgs, (Method[]) null); }
/** * Creates a strict mock object that supports mocking of final and native * methods and invokes a specific constructor. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArgs * The constructor arguments that will be used to invoke a * special constructor. * @param methods * optionally what methods to mock * @return the mock object. */ public static <T> T createStrictMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) { return doMock(type, false, new StrictMockStrategy(), constructorArgs, methods); }
/** * Creates a nice mock object that supports mocking of final and native * methods and invokes a specific constructor. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArgs * The constructor arguments that will be used to invoke a * special constructor. * @param methods * optionally what methods to mock * @return the mock object. */ public static <T> T createNiceMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) { return doMock(type, false, new NiceMockStrategy(), constructorArgs, methods); }
/** * Creates a strict mock object that supports mocking of final and native * methods and invokes a specific constructor based on the supplied argument * values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. * @return the mock object. */ public static <T> T createStrictMock(Class<T> type, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new StrictMockStrategy(), constructorArgs, (Method[]) null); }
/** * Creates a nice mock object that supports mocking of final and native * methods and invokes a specific constructor based on the supplied argument * values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. * @return the mock object. */ public static <T> T createNiceMock(Class<T> type, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new NiceMockStrategy(), constructorArgs, (Method[]) null); }
/** * A utility method that may be used to mock several methods in an easy way * (by just passing in the method names of the method you wish to mock). The * mock object created will support mocking of final methods and invokes the * default constructor (even if it's private). * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodNames * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @return the mock object. */ public static <T> T createPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception { return createMock(type, new ConstructorArgs(Whitebox.getConstructor(type)), Whitebox.getMethods(type, methodNames)); }
/** * A utility method that may be used to nicely mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). The mock object created will support mocking of final methods and * invokes the default constructor (even if it's private). * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodNames * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @return the mock object. */ public static <T> T createNicePartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception { return createNiceMock(type, new ConstructorArgs(Whitebox.getConstructor(type)), Whitebox.getMethods(type, methodNames)); }
/** * A utility method that may be used to strictly mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). The mock object created will support mocking of final methods and * invokes the default constructor (even if it's private). * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodNames * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @return the mock object. */ public static <T> T createStrictPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames) throws Exception { return createStrictMock(type, new ConstructorArgs(Whitebox.getConstructor(type)), Whitebox.getMethods(type, methodNames)); }
/** * A utility method that may be used to mock several methods in an easy way * (by just passing in the method names of the method you wish to mock). The * mock object created will support mocking of final and native methods and * invokes a specific constructor based on the supplied argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodNames * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. (optional) * @return the mock object. */ public static <T> T createPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new DefaultMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames)); }
/** * * A utility method that may be used to strictly mock several methods in * an easy way (by just passing in the method names of the method you wish * to mock). The mock object created will support mocking of final and * native methods and invokes a specific constructor based on the supplied * argument values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodNames * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. (optional) * @return the mock object. */ public static <T> T createStrictPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new StrictMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames)); }
/** * * A utility method that may be used to nicely mock several methods in an * easy way (by just passing in the method names of the method you wish to * mock). The mock object created will support mocking of final and native * methods and invokes a specific constructor based on the supplied argument * values. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param methodNames * The names of the methods that should be mocked. If * <code>null</code>, then this method will have the same effect * as just calling {@link #createMock(Class, Method...)} with the * second parameter as <code>new Method[0]</code> (i.e. all * methods in that class will be mocked). * @param constructorArguments * The constructor arguments that will be used to invoke a * certain constructor. (optional) * @return the mock object. */ public static <T> T createNicePartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) { Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments); ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments); return doMock(type, false, new NiceMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames)); }
/** * Creates a mock object that supports mocking of final and native methods * and invokes a specific constructor. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArgs * The constructor arguments that will be used to invoke a * special constructor. * @param methods * optionally what methods to mock * @return the mock object. */ protected final <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) { return PowerMock.createMock(type, constructorArgs, methods); }
/** * Creates a strict mock object that supports mocking of final and native * methods and invokes a specific constructor. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArgs * The constructor arguments that will be used to invoke a * special constructor. * @param methods * optionally what methods to mock * @return the mock object. */ protected final <T> T createStrictMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) { return PowerMock.createStrictMock(type, constructorArgs, methods); }
/** * Creates a nice mock object that supports mocking of final and native * methods and invokes a specific constructor. * * @param <T> * the type of the mock object * @param type * the type of the mock object * @param constructorArgs * The constructor arguments that will be used to invoke a * special constructor. * @param methods * optionally what methods to mock * @return the mock object. */ protected final <T> T createNiceMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) { return PowerMock.createNiceMock(type, constructorArgs, methods); }