Sorry for typical question but i'm new in Mockito and can't understand what a problem. And could you say me, how to do Mockito units correctly, because I really can't understand them! Thanx a lot! I have got next exception:
Wanted but not invoked: transactionManager.execute( ua.khpi.shapoval.autostation.dao.UsersServiceTest$$Lambda$1/717386707@335eadca ); -> at ua.khpi.shapoval.autostation.dao.UsersServiceTest.testGetUserByLogin(UsersServiceTest.java:42) Actually, there were zero interactions with this mock.
Here is my DAO class:
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;
}
}
Here is my Service class
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));
}
}
And here is my test class with Mockito
public class UsersServiceTest {
private static MySqlUsersService mockedUsersService;
private static TransactionManager transactionManager;
private static UsersDao usersDao;
private static Users user1;
@BeforeClass
public static void init() {
user1=mock(Users.class);
usersDao = mock(MySqlUsersDao.class);
transactionManager = mock(TransactionManager.class);
mockedUsersService = new MySqlUsersService(usersDao, transactionManager);
}
@Test
public void testGetUserByLogin() {
// Users user = mockedUsersService.getUserByLogin("user1");
when(usersDao.getUserByLogin("user1")).thenReturn(user1);
assertEquals(user1,mockedUsersService.getUserByLogin("user1"));
verify(transactionManager).execute(()->usersDao.getUserByLogin("user1"));
}
}
Aucun commentaire:
Enregistrer un commentaire