I Have the following structure of a class:
public class MyClass <T extends InterfaceA & InterfaceB> {
private final T someOtherClass;
public MyClass(T someOtherClass){
this.someOtherClass = someOtherClass;
}
}
Now I want to test this class and for that I need to mock the someOtherClass. Here is what I have for my tests:
@ExtendWith(MockitoExtension.class)
public class MyeTest {
//NewClass implements both interfaces: InterfaceA and InterfaceB
@Mock
private NewClass newClass;
@InjectMocks
private MyClass<NewClass> myClass;
@Test
void someTest() {
//...
}
Error Message that I get: org.mockito.exceptions.misusing.InjectMocksException: Cannot instantiate @InjectMocks field named 'myClass' of type MyClass. You haven't provided the instance at field declaration so I tried to construct the instance. However the constructor or the initialization block threw an exception : null
Within test I tried to add a new empty interface that extends both InterfaceA and InterfaceB, and use it instead of a NewClass that implements both interfaces, like that:
@Mock
private InterfaceAandB someName;
@InjectMocks
private MyClass<InterfaceAandB> myClass;
But I still get the same error message.