Permalink
Browse files

Fixed #849 PowerMock.replayAll does not handle mocks created by annot…

…ation processor for `org.easymock.Mock` annotation
  • Loading branch information...
thekingnothing committed Sep 16, 2017
1 parent 3876b78 commit 054becfc3ce2ebcc1cb37aaea8b5a62b4eccf204
@@ -1,6 +1,6 @@
ext {
bintrayRepo = 'maven'
bintrayAutoPublish = false
bintrayAutoPublish = true
mavenCentralSync = false
bintrayPublications = ['javaLibrary']
}
@@ -18,12 +18,14 @@
import org.powermock.api.easymock.EasyMockConfiguration;
import org.powermock.api.easymock.annotation.MockNice;
import org.powermock.api.easymock.annotation.MockStrict;
import org.powermock.core.MockRepository;
import org.powermock.core.classloader.annotations.Mock;
import org.powermock.core.spi.listener.AnnotationEnablerListener;
import org.powermock.core.spi.support.AbstractPowerMockTestListenerBase;
import org.powermock.reflect.Whitebox;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
@@ -69,6 +71,7 @@ public void beforeTestMethod(Object testInstance, Method method, Object[] argume
// first emulate default EasyMockRunner behavior
if (easyMockConfiguration.isInjectMocksSupported()) {
Whitebox.invokeMethod(Class.forName("org.easymock.EasyMockSupport"), "injectMocks", testInstance);
registerAllCreatedMocks(testInstance);
}
// then inject in empty fields mock created via PowerMock
@@ -80,4 +83,14 @@ public void beforeTestMethod(Object testInstance, Method method, Object[] argume
protected EasyMockAnnotationSupport getEasyMockAnnotationSupport(Object testInstance) {
return new EasyMockAnnotationSupport(testInstance);
}
private void registerAllCreatedMocks(final Object testInstance) {
final Field[] fields = testInstance.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(org.easymock.Mock.class)) {
Object value = Whitebox.getFieldValue(field, testInstance);
MockRepository.addObjectsToAutomaticallyReplayAndVerify(value);
}
}
}
}
@@ -864,5 +864,8 @@ public static void setInternalStateFromContext(Object instance, Object context,
public static void setInternalStateFromContext(Object instance, Class<?> context, FieldMatchingStrategy strategy) {
WhiteboxImpl.setInternalStateFromContext(instance, context, strategy);
}
public static Object getFieldValue(final Field field, final Object object) {
return WhiteboxImpl.getFieldValue(field, object);
}
}
@@ -2578,5 +2578,20 @@ private static void assertObjectInGetInternalStateIsNotNull(Object object) {
}
return converted;
}
public static Object getFieldValue(final Field field, final Object object) {
boolean access = field.isAccessible();
try {
field.setAccessible(true);
return field.get(object);
}catch (Exception e){
return null;
}finally {
try{
field.setAccessible(access);
}catch (Exception ignored){
}
}
}
}
@@ -1,4 +1,4 @@
package samples.junit412.github668;
package samples.junit412.bug.github668;
/**
*
@@ -1,4 +1,4 @@
package samples.junit412.github668;
package samples.junit412.bug.github668;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -14,9 +14,6 @@
import static org.powermock.api.easymock.PowerMock.replayAll;
/**
*
*/
@RunWith(PowerMockRunner.class)
@PowerMockListener(AnnotationEnabler.class)
public class TwoMockFieldsWithDifferentTypesClass {
@@ -1,4 +1,4 @@
package samples.junit412.github668;
package samples.junit412.bug.github668;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -2,4 +2,4 @@
* \@Mock fields are not injected anymore (1.6.5 regression)
* https://github.com/jayway/powermock/issues/668
*/
package samples.junit412.github668;
package samples.junit412.bug.github668;
@@ -0,0 +1,27 @@
package samples.junit412.bug.github849;
import org.easymock.Mock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.modules.junit4.PowerMockRunner;
import samples.Service;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
@RunWith(PowerMockRunner.class)
public class EasyMockAnnotationTest {
@Mock
private Service serviceMock;
@Test
public void replayAll_should_switch_to_replay_mode_mocks_created_by_easymock_annotation_processing() {
expect(serviceMock.getServiceMessage()).andReturn("value");
PowerMock.replayAll();
assertEquals("value", serviceMock.getServiceMessage());
}
}
@@ -0,0 +1,4 @@
/**
* PowerMock.replayAll does not handle mocks created by annotation processor for `org.easymock.Mock` annotation
*/
package samples.junit412.bug.github849;

This file was deleted.

Oops, something went wrong.

0 comments on commit 054becf

Please sign in to comment.