Skip to content

Commit

Permalink
Issue powermock#769: PowerMockMaker with Mockito2.
Browse files Browse the repository at this point in the history
  • Loading branch information
podarsmarty committed Mar 28, 2017
1 parent 152ba61 commit 2838de8
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
3 changes: 3 additions & 0 deletions powermock-api/powermock-api-mockito2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
testCompile(project(":tests:utils"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker;
import org.mockito.internal.stubbing.InvocationContainer;
import org.mockito.internal.util.MockNameImpl;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.MockMaker;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.internal.pluginswitch.PowerMockPluginSwitch;
import org.powermock.api.mockito.repackaged.CglibMockMaker;
import org.powermock.core.classloader.MockClassLoader;

Expand All @@ -40,10 +42,16 @@
* For more details see the {@link org.powermock.api.mockito.internal.invocation.ToStringGenerator}.
*/
public class PowerMockMaker implements MockMaker {

private static boolean isEnabled = true;
private final MockMaker cglibMockMaker = new CglibMockMaker();
private final MockMaker byteBuddyMockMaker = new ByteBuddyMockMaker();

@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
if(!isEnabled) {
return byteBuddyMockMaker.createMock(settings, handler);
}
T mock = cglibMockMaker.createMock(settings, handler);
ClassLoader classLoader = cglibMockMaker.getClass().getClassLoader();
if (classLoader instanceof MockClassLoader) {
Expand All @@ -56,8 +64,9 @@ public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {

@Override
public MockHandler getHandler(Object mock) {
// Return a fake mock handler for static method mocks
if (mock instanceof Class) {
if(!isEnabled) {
return byteBuddyMockMaker.getHandler(mock);
} else if (mock instanceof Class) { // Return a fake mock handler for static method mocks
return new PowerMockInternalMockHandler((Class<?>) mock);
} else {
return cglibMockMaker.getHandler(mock);
Expand All @@ -66,11 +75,18 @@ public MockHandler getHandler(Object mock) {

@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
if(!isEnabled) {
byteBuddyMockMaker.resetMock(mock, newHandler, settings);
return;
}
cglibMockMaker.resetMock(mock, newHandler, settings);
}

@Override
public TypeMockability isTypeMockable(Class<?> type) {
if (!isEnabled) {
byteBuddyMockMaker.isTypeMockable(type);
}
return cglibMockMaker.isTypeMockable(type);
}

Expand Down Expand Up @@ -106,4 +122,20 @@ public Object handle(Invocation invocation) throws Throwable {
return null;
}
}

/**
* @deprecated Please use {@link PowerMockPluginSwitch#disablePowerMockMaker} instead
*/
@Deprecated
public static void disablePowerMockMaker() {
isEnabled = false;
}

/**
* @deprecated Please use {@link PowerMockPluginSwitch#enablePowerMockMaker} instead
*/
@Deprecated
public static void enablePowerMockMaker() {
isEnabled = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.powermock.api.mockito.internal.pluginswitch;

import org.mockito.Mockito;
import org.mockito.plugins.PluginSwitch;
import org.powermock.api.mockito.internal.mockmaker.PowerMockMaker;

public class PowerMockPluginSwitch implements PluginSwitch {

@Override
public boolean isEnabled(String pluginClassName) {
return true;
}

public static void disablePowerMockMaker() {
PowerMockMaker.disablePowerMockMaker();
Mockito.reset();
}

public static void enablePowerMockMaker() {
PowerMockMaker.enablePowerMockMaker();
Mockito.reset();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.powermock.api.mockito.internal.pluginswitch;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.exceptions.misusing.MissingMethodInvocationException;
import samples.classwithnonpublicparent.ClassWithNonPublicParent;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PowerMockPluginSwitchTest {

private String mSomeOtherString = "some other string";

@After
@Before
public void setup() {
PowerMockPluginSwitch.enablePowerMockMaker();
}

@Test(expected=MissingMethodInvocationException.class)
public void test_PowerMockMakerEnabled_Throws() {
PowerMockPluginSwitch.enablePowerMockMaker();
ClassWithNonPublicParent mockClassWithNonPublicParent = mock(ClassWithNonPublicParent.class);
when(mockClassWithNonPublicParent.getSomeStringFromPackageProtectedClass()).thenReturn(mSomeOtherString);
PowerMockPluginSwitch.disablePowerMockMaker();
}

@Test
public void test_PowerMockMakerDisabled_MocksMethod() {
PowerMockPluginSwitch.disablePowerMockMaker();
ClassWithNonPublicParent mockClassWithNonPublicParent = mock(ClassWithNonPublicParent.class);
when(mockClassWithNonPublicParent.getSomeStringFromPackageProtectedClass()).thenReturn(mSomeOtherString);
Assert.assertEquals(mSomeOtherString, mockClassWithNonPublicParent.getSomeStringFromPackageProtectedClass());
}

@Test
public void test_PowerMockMaker_SwitchesCorrectly() {
test_PowerMockMakerDisabled_MocksMethod();
try {
test_PowerMockMakerEnabled_Throws();
} catch (MissingMethodInvocationException e) {
return;
}
assertTrue("Expected exception not thrown.", false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.powermock.api.mockito.internal.pluginswitch.PowerMockPluginSwitch
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package samples.classwithnonpublicparent;

public class ClassWithNonPublicParent extends PackageProtectedClass {}

class PackageProtectedClass {
public String getSomeStringFromPackageProtectedClass() {
return "some string";
}
}

0 comments on commit 2838de8

Please sign in to comment.