Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject mocks to Abstract class using mockito

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.

like image 829
Dilini Rajapaksha Avatar asked Aug 21 '15 05:08

Dilini Rajapaksha


2 Answers

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.

like image 176
Craig Avatar answered Sep 20 '22 03:09

Craig


You can use the Powermock library to inject mocks in myAbstractClass using Whitebox.setInternalState(myAbstractClass, mock(MockedClass.class));

like image 31
Sparwer Avatar answered Sep 19 '22 03:09

Sparwer