當我爲下面的類運行單元測試時,我得到註釋所指示的NullPointerException。我有一種感覺,我在這裏錯過了一些簡單的東西。我正在使用Mockito 1.9.5,所有嘲笑的方法都不是最終的。調用模擬類的方法時出現NullPointerException
public class UserBOTest {
@Mock private IUserDAO userDao;
@InjectMocks private IUserBO userBo = new UserBO();
@Test
public void testRegister() throws DBException {
when(userDao.doesEmailExist(any(String.class))).thenReturn(false); //NULLPOINTEREXCEPTION HERE
when(userDao.saveOrUpdate(Matchers.<User>any())).thenReturn(true);
Assert.assertEquals(userBo.registerUser("user", "pwd", "email", ""), Constants.General.OK);
}
}
堆棧跟蹤:
java.lang.NullPointerException
at com.dbcreator.services.bo.UserBOTest.testRegister(UserBOTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
是的,那正是我一直想念的。我忘了包含@RunWith(MockitoJUnitRunner.class)。謝謝。 – Zyga