Skip to content
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

#3000: fix ArrayIndexOutOfBoundsException #3190

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ protected boolean isCompatibleTypes(Type typeToMock, Type mockType, Field inject
ParameterizedType genericMockType = (ParameterizedType) mockType;
Type[] actualTypeArguments = genericTypeToMock.getActualTypeArguments();
Type[] actualTypeArguments2 = genericMockType.getActualTypeArguments();
// Recurse on type parameters, so we properly test whether e.g. Wildcard bounds
// have a match
result =
recurseOnTypeArguments(
injectMocksField, actualTypeArguments, actualTypeArguments2);
if (actualTypeArguments.length == actualTypeArguments2.length) {
// Recurse on type parameters, so we properly test whether e.g. Wildcard
// bounds have a match
result =
recurseOnTypeArguments(
injectMocksField,
actualTypeArguments,
actualTypeArguments2);
} else {
// the two ParameterizedTypes cannot match because they have unequal
// number of type arguments
result = false;
}
}
} else {
// mockType is a non-parameterized Class, i.e. a concrete class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.MockitoAnnotations.*;

import java.io.Serializable;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -360,4 +361,69 @@ public void testNoNpe() {
assertSame(innerList, spiedImpl.changes);
}
}

/* cannot define interface in nested test class RegressionArrayIndexOutOfBoundsExcepton ,
* because interface cannot be defined in a non-static inner class,
* and nested test class must be non-static in order for JUnit 5 to be able to run it */
public interface BaseRepository<E, I extends Serializable> {
E findById(I id);

E save(E entity);
}

/**
* Verify regression https://github.com/mockito/mockito/issues/3000 is fixed.
*/
@Nested
public class RegressionArrayIndexOutOfBoundsException {

public class BaseService<E, I extends Serializable> {
private BaseRepository<E, I> repository;
}

public class OneRepository implements BaseRepository<Map<String, String>, String> {
public Map<String, String> findById(String id) {
return Map.of();
}

public Map<String, String> save(Map<String, String> entity) {
return entity;
}
}

public class TwoRepository implements BaseRepository<Map<Integer, String>, String> {
public Map<Integer, String> findById(String id) {
return Map.of();
}

public Map<Integer, String> save(Map<Integer, String> entity) {
return entity;
}
}

public class One {}
;

public class Two implements Serializable {
private static final long serialVersionUID = 1L;
}

public class UnderTest extends BaseService<One, Two> {
private OneRepository oneRepository;
private TwoRepository twoRepository;
}

@Mock OneRepository oneRepository;

@Mock TwoRepository twoRepository;

@InjectMocks UnderTest underTest = new UnderTest();

@Test
public void testNoAioobe() {
assertNotNull(oneRepository);
assertNotNull(twoRepository);
assertNotNull(underTest);
}
}
}