forked from powermock/powermock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue powermock#769: PowerMockMaker with Mockito2.
- Loading branch information
1 parent
152ba61
commit 2838de8
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies { | ||
testCompile(project(":tests:utils")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
.../src/main/java/org/powermock/api/mockito/internal/pluginswitch/PowerMockPluginSwitch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../test/java/org/powermock/api/mockito/internal/pluginswitch/PowerMockPluginSwitchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...rmock-api-mockito2/src/test/resources/mockito-extensions/org.mockito.plugins.PluginSwitch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.powermock.api.mockito.internal.pluginswitch.PowerMockPluginSwitch |
9 changes: 9 additions & 0 deletions
9
tests/utils/src/main/java/samples/classwithnonpublicparent/ClassWithNonPublicParent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |