我是Mockito的新手,无法理解这是什么问题。你能告诉我,如何正确地做Mockito单位吗,因为我真的不懂它们!
我有下一个例外:
这是我的DAO类:java.lang.NullPointerException在ua.khpi.shapoval.autostation.service.MySqlUsersService.getUserIdByLogin (MySqlUsersService.java: 49)在ua.khpi.shapoval.autostation.dao.UsersServiceTest.testGetUserByLogin (UsersServiceTest.java: 57)在sun.reflect.NativeMethodAccessorImpl。invoke0(本地方法)sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java: 62)sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java: 43)java.lang.reflect.Method.invoke (Method.java: 498)org.junit.runners.model.FrameworkMethod runreflectivecall 1.美元(FrameworkMethod.java: 50)org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable.java: 12)在org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java: 47)在org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java: 17)在org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java: 26)org.junit.runners.ParentRunner.runLeaf (ParentRunner.java: 325)org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 78)在org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 57)在org.junit.runners.ParentRunner 3.美元运行(ParentRunner.java: 290)在org.junit.runners.ParentRunner 1.美元计划(ParentRunner.java: 71)org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 288)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 58)org.junit.runners.ParentRunner评估2.美元(ParentRunner.java: 268)org.junit.runners.ParentRunner.run (ParentRunner.java: 363)org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java: 86)在org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java: 38)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java: 459)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java: 675)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.java: 382)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java: 192)
public class MySqlUsersDao implements UsersDao {
/** Logger. */
private static final Logger LOG = Logger.getLogger(MySqlUsersDao.class);
/*
* (non-Javadoc)
*
* @see
* ua.khpi.shapoval.autostation.dao.UsersDao#getUserIdByLogin(java.lang.
* String)
*/
@Override
public int getUserIdByLogin(String login) {
Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
connection = DbConnector.getConnection();
stmt = connection.prepareStatement(Constants.UsersDao.SELECT_USER_ID_BY_LOGIN);
stmt.setString(1, login);
rs = stmt.executeQuery();
while (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
LOG.error(e.getMessage());
return 0;
} finally {
DbConnector.close(connection, stmt, rs);
}
return 0;
}
}
这是我的Service类
public class MySqlUsersService implements UsersService {
private UsersDao usersDao;
private TransactionManager transactionManager;
public MySqlUsersService(UsersDao usersDao, TransactionManager transactionManager) {
this.usersDao = usersDao;
this.transactionManager = transactionManager;
}
@Override
public int getUserIdByLogin(String login) {
return this.transactionManager.execute(() -> this.usersDao.getUserIdByLogin(login));
}
}
这是Mockito的测试类
public class UsersServiceTest {
@InjectMocks
private MySqlUsersService mockedUsersService;
@Mock
private static TransactionManager transactionManager;
@Mock
private static UsersDao usersDao;
@Mock
private static Users user1;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test public void testGetUserByLogin() {
when(usersDao.getUserByLogin("user1")).thenReturn(user1);
assertEquals(user1, mockedUsersService.getUserIdByLogin("user1"));
verify(transactionManager).execute(()->usersDao.getUserIdByLogin("user1"));
}
}
如果我理解正确的话
verify(transactionManager).execute(()->usersDao.getUserByLogin("user1"));
这是因为函数是通过引用进行比较的,在测试中传递的函数和在实现中传递的函数是不同的实例。
我建议您使用ArgumentCaptor
捕获函数。调用捕获的函数并验证它是否按照您的期望执行。
错误消息告诉您验证语句没有在testGetUserByLogin()
中执行。
我认为你的实际问题是测试类应该在你的测试类中注释@ injectmock。这个注释告诉Mockito将mock注入到哪个类中:
@InjectMocks
private MySqlUsersService mockedUsersService
我会这样重写你的测试:
public class UsersServiceTest {
@InjectMocks
private MySqlUsersService mockedUsersService;
@Mock
private static TransactionManager transactionManager;
@Mock
private static UsersDao usersDao;
@Mock
private static Users user1;
@Before
public static void init() { initMocks(this); }
@Test public void testGetUserByLogin() {
when(usersDao.getUserByLogin("user1")).thenReturn(user1);
assertEquals(user1, mockedUsersService.getUserIdByLogin("user1"));
verify(transactionManager).execute(()->usersDao.getUserIdByLogin("user1"));
} }