Using Mockito or another similar framework. Is there a way to mock a package-private class?
I am trying to test my service layer mocking my DAO classes. The problem is that the DAO instances are package private and only can be obtained through a factory.
MyPackagePrivateDao mockedDao = mock(MyPackagePrivateDao.class);
The compiler says that the class cannot be accessed from outside the package.
Do you have any example?
Thanks
This is not possible with Mockito, it requires to modify the bytecode of the actual class. This is not a planned feature.
Don’t you have interfaces that you could eventually mock for these DAOs ?
Another option is to look on PowerMock which is great to deal with legacy code, ie when the software design is forcing you to mock statics, private, final, etc.