You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Version Information
Java Version - 17
Mockito Version - 4.0.0
Spring Boot Version - 2.6.10
System Info- MacOS#12.6#Apple M1 Pro
Issue Description
I'm working on the task of migrating my project to Java 17 and Spring boot 2.6.10.
In the existing test cases, we're spying a hashmap that has some values in it.
With Java 17 and mockito (4.0.0), mockito's spy is not working properly and is returning an empty hashmap.
So after looking at #2589 , I added mockito-inline as an extra dependency in the project.
Now, it's able to spy the hashmap with all the relevant values but it's adding an extra entry in the Map -
Key- MethodHandle(Object)int
value - int array
Exception Message-
java.lang.ClassCastException: class java.lang.invoke.BoundMethodHandle$Species_LI cannot be cast to class java.util.UUID (java.lang.invoke.BoundMethodHandle$Species_LI and java.util.UUID are in module java.base of loader 'bootstrap')
at java.base/java.util.HashMap.forEach(HashMap.java:1421)
at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
I'm able to reproduce this issue in a small dummy project. Let me know If any extra information is required here.
NOTE - This same sample project and my application project work fine with the same configuration on Java 11.
Required Files of the sample project-
a) Domain-
import java.util.UUID;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class RandomFeed {
private UUID id;
private String subject;
}
b) Service layer-
@Service
public class RandomFeedService {
public Map<UUID, List<RandomFeed>> groupFeedById(List<RandomFeed> newsFeedEntries) {
Map<UUID, List<RandomFeed>> randomEntries = new HashMap<>();
newsFeedEntries.forEach(randomFeedEntry -> {
final UUID accountId = randomFeedEntry.getId();
if (!randomEntries.containsKey(accountId)) {
randomEntries.put(accountId, new ArrayList<>());
}
randomEntries.get(accountId).add(randomFeedEntry);
});
return randomEntries;
}
}
c) Test file-
@SpringBootTest
class DemoApplicationTests {
@InjectMocks
private RandomFeedService randomFeedService;
@Test
void test() {
List<RandomFeed> newsFeedEntries = fetchData();
Map<UUID, List<RandomFeed>> newsEntries = spy(randomFeedService.groupFeedById(newsFeedEntries));
newsEntries.forEach((k, v) -> {
// It Fails here since one of the entry in this map is not of type - UUID.
});
}
List<RandomFeed> fetchData() {
return Arrays.asList(
RandomFeed.builder().id(UUID.randomUUID()).subject("Instance 1").build(),
RandomFeed.builder().id(UUID.randomUUID()).subject("Instance 2").build());
}
}
Unfortunately, HashMap is one of the classes that Mockito relies on internally for its behavior. Stubbing HashMap will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.
Version Information
Java Version - 17
Mockito Version - 4.0.0
Spring Boot Version - 2.6.10
System Info- MacOS#12.6#Apple M1 Pro
Issue Description
I'm working on the task of migrating my project to Java 17 and Spring boot 2.6.10.
In the existing test cases, we're spying a hashmap that has some values in it.
With Java 17 and mockito (4.0.0), mockito's spy is not working properly and is returning an empty hashmap.
So after looking at #2589 , I added mockito-inline as an extra dependency in the project.
Now, it's able to spy the hashmap with all the relevant values but it's adding an extra entry in the Map -
Key- MethodHandle(Object)int
value - int array
Exception Message-
I'm able to reproduce this issue in a small dummy project. Let me know If any extra information is required here.
NOTE - This same sample project and my application project work fine with the same configuration on Java 11.
Required Files of the sample project-
a) Domain-
b) Service layer-
c) Test file-
d) pom file-
The text was updated successfully, but these errors were encountered: