Permalink
Browse files

Fixed #755 @Mock annotation from easymock api does not work for two f…

…ields of the same type.
  • Loading branch information...
thekingnothing committed Sep 15, 2017
1 parent a9f82fb commit 37b7e5bd17202f8043ec4f81eebfd550bf5f2c63
View
@@ -10,4 +10,5 @@ repo
.idea
**/build/**
.gradle
**/classes/**
**/classes/**
**/out/**
@@ -31,13 +31,15 @@
private final String qualifier;
private final Class<? extends Annotation> annotation;
private final Annotation annotationInstance;
private final String fieldName;
private Object mock;
public AnnotationMockMetadata(Class<? extends Annotation> annotation, Field field) throws
Exception {
this.annotation = annotation;
this.annotationInstance = field.getAnnotation(annotation);
this.type = field.getType();
this.fieldName = field.getName();
this.methods = getMethod();
this.qualifier = findQualifier();
}
@@ -57,7 +59,12 @@ private String findQualifier() {
return fieldName;
}
}
@Override
public String getFieldName() {
return fieldName;
}
@Override
public String getQualifier() {
return qualifier;
@@ -44,9 +44,20 @@ public Field findField(Object instance, MockMetadata mockMetadata) {
if (candidates.size() == 1) {
return candidates.iterator().next();
}
candidates = filterByFieldName(candidates, mockMetadata.getFieldName());
if (candidates.size() == 1) {
return candidates.iterator().next();
}
return null;
}
private Set<Field> filterByFieldName(final Set<Field> candidates, final String fieldName) {
if (fieldName == null || fieldName.length() == 0) {
return candidates;
}
return doFilterByQualifier(candidates, fieldName);
}
private Set<Field> filterByType(Set<Field> candidates, Class<?> type) {
if (type == null) {
return candidates;
@@ -24,6 +24,8 @@
*
*/
public interface MockMetadata {
String getFieldName();
String getQualifier();
Class<? extends Annotation> getAnnotation();
@@ -39,14 +39,10 @@
@Test
public void injectMocksWorksWithPowermockMock() {
assertNotNull("dependencyHolder is null", dependencyHolder.getInjectDemo());
}
@Test
public void testInjectDemoSay() throws Exception {
public void should_inject_mock_without_quantifier() throws Exception {
InjectDemo tested = dependencyHolder.getInjectDemo();
final InjectDemo tested = dependencyHolder.getInjectDemo();
assertNotNull("dependencyHolder is null", tested);
String expected = "Hello altered World";
expectPrivate(tested,"say","hello").andReturn("Hello altered World");
@@ -58,14 +54,10 @@ public void testInjectDemoSay() throws Exception {
}
@Test
public void injectMocksWorksWithPowermockMockWithQualifier() {
assertNotNull("dependencyHolder is null", dependencyHolderQualifier.getInjectDemoQualifier());
}
@Test
public void testInjectDemoQualifier() throws Exception {
public void should_inject_mock_with_quantifier() throws Exception {
InjectDemo tested = dependencyHolderQualifier.getInjectDemoQualifier();
assertNotNull("dependencyHolderQualifier is null", tested);
String expected = "Hello altered World";
expectPrivate(tested,"say","hello").andReturn("Hello altered World");
@@ -0,0 +1,40 @@
package samples.junit412.bug.github755;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.api.easymock.annotation.MockNice;
import org.powermock.modules.junit4.PowerMockRunner;
import samples.newmocking.SomeDependency;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(PowerMockRunner.class)
public class TwoObjectsAnnotatedTest {
@Mock
private String obj1;
@Mock
private String obj2;
@MockNice
private SomeDependency someClass1;
@MockNice
private SomeDependency someClass2;
@Test
public void should_create_mock_for_all_fields_annotated_Mock() {
assertThat(obj1).isNotNull();
assertThat(obj2).isNotNull();
}
@Test
public void should_create_mock_for_all_fields_annotated_MockNice() {
assertThat(someClass1).isNotNull();
assertThat(someClass2).isNotNull();
}
}
@@ -0,0 +1,5 @@
/**
* @Mock annotation from easymock api does not work for two fields of the same type.
*
*/
package samples.junit412.bug.github755;

0 comments on commit 37b7e5b

Please sign in to comment.