I am using Java 8. when I am trying to return a list of type Items(user defined), it is throwing class cast exception. can you please tell me what I am doing wrong?
here's the code.
@ExtendWith(MockitoExtension.class)
public class ItemsMicroserviceApplicationServiceTests {
@Mock
ItemRepo itemrepo;
@InjectMocks
ItemsService itemsService=new ItemsService();
List<Items> itemList;
@Test
void testaddItemsService()
{
itemList=new ArrayList<>();
itemList.add(new Items(1,"oneplus","good smartwatch",5999));
itemList.add(new Items(2,"realme","decent buds",3999));
ItemListDto itemListDto=new ItemListDto(itemList);
Optional<List<Items>> optionalList=Optional.of(itemList);
when(itemsService.addItemsService(itemListDto)).thenReturn(optionalList);
assertTrue(true);
}
}
I have used assertTrue just to avoid the error but still I am getting it.
This is the error I am getting.
java.lang.ClassCastException: class java.util.Optional cannot be cast to class com.example.items.model.Items (java.util.Optional is in module java.base of loader 'bootstrap'; com.example.items.model.Items is in unnamed module of loader 'app')
at com.example.items.repository.ItemRepo$MockitoMock$frrDWmzA.save(Unknown Source)
at com.example.items.repository.ItemRepo$MockitoMock$frrDWmzA.save(Unknown Source)
at com.example.items.service.ItemsService.lambda$0(ItemsService.java:45)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
at com.example.items.service.ItemsService.addItemsService(ItemsService.java:45)
at com.example.items.demo.service.ItemsMicroserviceApplicationServiceTests.testaddItemsService(ItemsMicroserviceApplicationServiceTests.java:56)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)