I have a dao.create()
call that I want to mock when testing a method.
But I am missing something as I'm still getting NPE. What is wrong here?
class MyService {
@Inject
private Dao dao;
public void myMethod() {
//..
dao.create(object);
//
}
}
How can I mock out the dao.create() call?
@RunWith(PowerMockRunner.class)
@PrepareForTest(DAO.class)
public void MyServiceTest {
@Test
public void testMyMethod() {
PowerMockito.mock(DAO.class);
MyService service = new MyService();
service.myMethod(); //NPE for dao.create()
}
}
As others have already said, you need to set the
dao
field in yourMyService
class in some fashion. I'm unsure the mechanism to allow for a compound runner on your test to use both Powermock and a DI framework runner (assuming Powermock is required), but as long as you're already using PowerMock (for reasons unclear in the given example), you could avail yourself of the Whitebox class to set the dao more manually.You still need to set the dao field with your mock. You can use reflection to this.
You are not injecting the DAO. With mockito you can change your test class to use @InjectMocks and use mockito runner.
You can read more about InjectMocks at Inject Mocks API
Simpler way is changing your injection to injection by constructor. For example, you would change MyService to
then your test you could simple pass the mocked DAO in setup.
now you can use
verify
to check ifcreate
was called, like:Using injection by constructor will let your dependencies clearer to other developers and it will help when creating tests :)
If you use
new MyService()
the Dao is never injected. For the Dao to be injected you need to load the MyService via anApplicationContext
(Spring) or anInjector
(Guice). Like you would in your normal application.You need to inject/set the mocked object DAO in your service class.
If it is a spring based project, you may have a look @ Spring Junit Testrunner