Skip to content

Commit

Permalink
Fixed #755 @mock annotation from easymock api does not work for two f…
Browse files Browse the repository at this point in the history
…ields of the same type.
  • Loading branch information
Arthur Zagretdinov authored and thekingn0thing committed Sep 16, 2017
1 parent 2f7f356 commit 86f4ac3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ repo
**/build/**
.gradle
**/out/**
**/classes/**
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public class AnnotationMockMetadata implements MockMetadata {
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();
}
Expand All @@ -57,7 +59,12 @@ private String findQualifier() {
return fieldName;
}
}


@Override
public String getFieldName() {
return fieldName;
}

@Override
public String getQualifier() {
return qualifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*
*/
public interface MockMetadata {
String getFieldName();

String getQualifier();

Class<? extends Annotation> getAnnotation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ public class TestSubjectPowermockAnnotationTest {


@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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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 86f4ac3

Please sign in to comment.