-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes issue #1917 #3154
Open
sectorpre
wants to merge
4
commits into
mockito:main
Choose a base branch
from
sectorpre:stopping-tests-from-failing
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fixes issue #1917 #3154
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -9,14 +9,8 @@ | |
import org.mockito.internal.util.MockUtil; | ||
import org.mockito.plugins.MemberAccessor; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.lang.reflect.*; | ||
import java.util.*; | ||
|
||
import static java.lang.reflect.Modifier.isStatic; | ||
|
||
|
@@ -165,6 +159,7 @@ public interface ConstructorArgumentResolver { | |
* @return The argument instances to be given to the constructor, should not be null. | ||
*/ | ||
Object[] resolveTypeInstances(Class<?>... argTypes); | ||
Object[] resolveTypeInstancesGeneric(HashMap<String, Type> hashMap, Type[] types); | ||
} | ||
|
||
private interface ConstructorInstantiator { | ||
|
@@ -284,9 +279,17 @@ private int countMockableParams(Constructor<?> constructor) { | |
|
||
@Override | ||
public FieldInitializationReport instantiate() { | ||
Field[] fields = testClass.getClass().getDeclaredFields(); | ||
HashMap<String, Type> hashmap = new HashMap<>(); | ||
|
||
for (Field field : fields) { | ||
hashmap.put(field.getName(), field.getGenericType()); | ||
} | ||
final MemberAccessor accessor = Plugins.getMemberAccessor(); | ||
Constructor<?> constructor = biggestConstructor(field.getType()); | ||
final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); | ||
final Object[] args = argResolver.resolveTypeInstancesGeneric( | ||
hashmap, constructor.getGenericParameterTypes()); | ||
//final Object[] args = argResolver.resolveTypeInstances(constructor.getParameterTypes()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are removing the only usage of |
||
try { | ||
Object newFieldInstance = accessor.newInstance(constructor, args); | ||
accessor.set(field, testClass, newFieldInstance); | ||
|
66 changes: 66 additions & 0 deletions
66
...internal/configuration/injection/InjectionFourHashmapsWithDifferentTypeParameterTest.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,66 @@ | ||
package org.mockito.internal.configuration.injection; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.HashMap; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class InjectionFourHashmapsWithDifferentTypeParameterTest { | ||
public static class TwoListsTest { | ||
private final HashMap<String, String> hashmap1; | ||
private final HashMap<String, Integer> hashmap2; | ||
private final HashMap<Float, Integer> hashmap3; | ||
private final HashMap<Float, Float> hashmap4; | ||
|
||
public TwoListsTest( | ||
HashMap<String, String> hashmap1, HashMap<String, Integer> hashmap2, HashMap<Float, Integer> hashmap3, HashMap<Float, Float> hashmap4) { | ||
this.hashmap1 = hashmap1; | ||
this.hashmap2 = hashmap2; | ||
this.hashmap3 = hashmap3; | ||
this.hashmap4 = hashmap4; | ||
} | ||
|
||
public HashMap<String, String> getHashmap1() { | ||
return hashmap1; | ||
} | ||
|
||
public HashMap<String, Integer> getHashmap2() { | ||
return hashmap2; | ||
} | ||
|
||
public HashMap<Float, Integer> getHashmap3() { | ||
return hashmap3; | ||
} | ||
|
||
public HashMap<Float, Float> getHashmap4() { | ||
return hashmap4; | ||
} | ||
} | ||
|
||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
HashMap<String, String> mockhashmap1; | ||
|
||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
HashMap<String, Integer> mockhashmap2; | ||
|
||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
HashMap<Float, Float> mockhashmap4; | ||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
HashMap<Float, Integer> mockhashmap3; | ||
|
||
@InjectMocks | ||
TwoListsTest productionClass; | ||
@Test | ||
public void test() { | ||
assertEquals("mockhashmap1", productionClass.getHashmap1().toString()); | ||
assertEquals("mockhashmap2", productionClass.getHashmap2().toString()); | ||
assertEquals("mockhashmap3", productionClass.getHashmap3().toString()); | ||
assertEquals("mockhashmap4", productionClass.getHashmap4().toString()); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ito/internal/configuration/injection/InjectionTwoListsWithDifferentTypeParameterTest.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,46 @@ | ||
package org.mockito.internal.configuration.injection; | ||
|
||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.List; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class InjectionTwoListsWithDifferentTypeParameterTest { | ||
public static class TwoListsTest { | ||
private final List<Integer> integerList; | ||
private final List<String> stringList; | ||
|
||
TwoListsTest(List<String> argumentStringList, List<Integer> argumentIntegerList) { | ||
this.stringList = argumentStringList; | ||
this.integerList = argumentIntegerList; | ||
} | ||
|
||
public List<Integer> getIntegerList() { | ||
return integerList; | ||
} | ||
|
||
public List<String> getStringList() { | ||
return stringList; | ||
} | ||
} | ||
@InjectMocks | ||
TwoListsTest productionClass; | ||
|
||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
List<Integer> mockIntegerList; | ||
|
||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
List<String> mockStringList; | ||
@Test | ||
public void test() { | ||
System.out.println(productionClass.getIntegerList()); | ||
System.out.println(productionClass.getStringList()); | ||
assertEquals("mockIntegerList", productionClass.getIntegerList().toString()); | ||
assertEquals("mockStringList", productionClass.getStringList().toString()); | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this hashmap? You are comparing objects with strings in the hashmap in the implementation, but that can lead to false-positives. I am also not sure why we would do that lookup there, as we can also pass in all the fields of the testclass into the lookup and perform the injection there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi thanks for the feedback! Definitely do agree that the hashmap is quite redundant but there's an issue storing mocks as a set of Objects, as this causes type erasure. I'll try to remove this hashmap in the next commit as well as store mocks as a set of Types instead of Objects.