I am mocking an abstract class like below:
myAbstractClass = Mockito.mock(MyAbstractClass.class, Mockito.CALLS_REAL_METHODS);
the problem is that MyAbstractClass
has some dependencies injected via EJB
annotations and there are not setters. Is there a way to inject the dependencies?
@InjectMocks
does not work with Abstract classes.
Since you cannot instantiate an Abstract class there is nothing to test. I would recommend that you create child class (it could be a nested class inside your test class), and then run your tests that way. Then you can use the @Mock
, @InjectMocks
as you would normally.
You can use the Powermock library to inject mocks in myAbstractClass
using Whitebox.setInternalState(myAbstractClass, mock(MockedClass.class))
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With