Search in sources :

Example 1 with MocksControl

use of org.easymock.internal.MocksControl in project powermock by powermock.

the class PowerMock method doMock.

@SuppressWarnings("unchecked")
private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy, ConstructorArgs constructorArgs, Method... methods) {
    if (type == null) {
        throw new IllegalArgumentException("The class to mock cannot be null");
    }
    /*
         * Clear the EasyMock state after the test method is executed.
         */
    MockRepository.addAfterMethodRunner(new Runnable() {

        @Override
        public void run() {
            LastControl.reportLastControl(null);
        }
    });
    IMocksControl control = mockStrategy.createMockControl(type);
    MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());
    T mock;
    if (type.isInterface()) {
        mock = control.createMock(type);
    } else if (type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers())) {
        Class<?> replicaType = createReplicaType(type, isStatic, constructorArgs);
        final Object replica = doCreateMock(replicaType, constructorArgs, control, methods);
        control = mockStrategy.createMockControl(replicaType);
        MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
        final Set<Method> methodsToMock = toSet(methods);
        if (isStatic) {
            MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
            MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
            return null;
        } else {
            final T newInstance;
            if (constructorArgs == null) {
                newInstance = Whitebox.newInstance(type);
                DefaultFieldValueGenerator.fillWithDefaultValues(newInstance);
            } else {
                try {
                    newInstance = (T) constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs());
                } catch (Exception e) {
                    throw new RuntimeException("Internal error", e);
                }
            }
            MockRepository.putInstanceMethodInvocationControl(newInstance, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
            if (!(newInstance instanceof InvocationSubstitute<?>)) {
                MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance);
            }
            return newInstance;
        }
    } else {
        mock = doCreateMock(type, constructorArgs, control, methods);
    }
    MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
    final Set<Method> methodsToMock = toSet(methods);
    if (isStatic) {
        MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h, methodsToMock, mock));
        MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
    } else {
        MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h, methodsToMock));
        if (!(mock instanceof InvocationSubstitute<?>)) {
            MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);
        }
    }
    ClassLoader classLoader = mock.getClass().getClassLoader();
    if (classLoader instanceof MockClassLoader) {
        ((MockClassLoader) classLoader).cache(mock.getClass());
    }
    return mock;
}
Also used : IMocksControl(org.easymock.IMocksControl) MocksControl(org.easymock.internal.MocksControl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) EasyMockMethodInvocationControl(org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl) IMocksControl(org.easymock.IMocksControl) MockInvocationHandler(org.easymock.internal.MockInvocationHandler) InvocationSubstitute(org.powermock.core.spi.support.InvocationSubstitute) MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Example 2 with MocksControl

use of org.easymock.internal.MocksControl in project powermock by powermock.

the class PowerMock method doCreateMock.

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(null, type, null, methods);
        }
    } else {
        if (methods == null) {
            mock = mocksControl.createMock(null, type, constructorArgs);
        } else {
            mock = mocksControl.createMock(null, type, constructorArgs, methods);
        }
    }
    return mock;
}
Also used : IMocksControl(org.easymock.IMocksControl) MocksControl(org.easymock.internal.MocksControl)

Example 3 with MocksControl

use of org.easymock.internal.MocksControl in project powermock by powermock.

the class MockDateTest method testMockDateWithEasyMock.

@Test
public void testMockDateWithEasyMock() throws Exception {
    Date someDate = new Date();
    MocksControl c = (MocksControl) org.easymock.EasyMock.createControl();
    Date date = c.createMock(Date.class);
    EasyMock.expect(date.after(someDate)).andReturn(false);
    PowerMock.replay(date);
    date.after(someDate);
    PowerMock.verify(date);
}
Also used : MocksControl(org.easymock.internal.MocksControl) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with MocksControl

use of org.easymock.internal.MocksControl in project powermock by powermock.

the class MockDateTest method testMockDateWithEasyMockFails.

@Test(expected = IllegalStateException.class)
public void testMockDateWithEasyMockFails() {
    Date someDate = new Date();
    MocksControl c = (MocksControl) org.easymock.EasyMock.createControl();
    Date date = c.createMock(null, Date.class, null);
    EasyMock.expect(date.after(someDate)).andReturn(false);
    Assert.fail("EasyMock with no methods mocked should not be possible to mock");
}
Also used : MocksControl(org.easymock.internal.MocksControl) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with MocksControl

use of org.easymock.internal.MocksControl in project intellij-community by JetBrains.

the class PyPullUpInfoModelTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    myFixture.configureByFile("/refactoring/pullup/pyPullUpInfoModel.py");
    final PyClass childClass = getClassByName("ChildWithDependencies");
    final PyClass parentClass = getClassByName("SomeParent");
    mySut = new PyPullUpInfoModel(childClass, new MocksControl(MocksControl.MockType.NICE).createMock(PyPullUpView.class));
    mySut.setSuperClass(parentClass);
    myMemberInfos = new PyMemberInfoStorage(childClass).getClassMemberInfos(childClass);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MocksControl(org.easymock.internal.MocksControl) PyMemberInfoStorage(com.jetbrains.python.refactoring.classes.PyMemberInfoStorage)

Aggregations

MocksControl (org.easymock.internal.MocksControl)6 Date (java.util.Date)2 IMocksControl (org.easymock.IMocksControl)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 PyClass (com.jetbrains.python.psi.PyClass)1 PyMemberInfoStorage (com.jetbrains.python.refactoring.classes.PyMemberInfoStorage)1 MockInvocationHandler (org.easymock.internal.MockInvocationHandler)1 EasyMockMethodInvocationControl (org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1 InvocationSubstitute (org.powermock.core.spi.support.InvocationSubstitute)1