我已经访问了许多博客和网站,以了解如何使用Mockito在Hibernate中测试我的DAO方法,但是我还没有找到可以帮助我编写代码的特定示例。我发现的是我必须使用集成测试而不是JUnit测试,但是我真的不知道如何用我的代码来实现。
问题::如何才能尽可能好地测试DAO方法?
我的代码:
我的仅包含模拟部分的测试:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class UserDAOTest
{
@Mock
private UserDAO userDAO;
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
}
@Test
public void testAddUser_AddsNewUser()
{
}
@Test
public void testDeleteUser_DeletesUser()
{
}
@Test
public void testGetUser_FetchUser()
{
}
@Test
public void testGetUsers_FetchesAllUsers()
{
}
}
我的UserDAO:
import Hibernate.HibernateUtil;
import Hibernate.Models.User;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Repository;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
//@Transactional
@Repository
public class UserDAO extends GeneralDAO
{
public void addUser(User user)
{
add(user);
}
/**
* Deletes a user from the database based on the userID
* @param userID
*/
public void deleteUser(int userID)
{
User user = new User();
delete(userID, user);
}
public User getUser(int userID) throws Exception
{
Transaction transaction = null;
User user = null;
try (Session session = HibernateUtil.getSessionFactory().openSession())
{
// start a transaction
transaction = session.beginTransaction();
// Gets the user object
user = session.get(User.class, userID);
// commit transaction
transaction.commit();
//closing session
session.close();
return user;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
return user;
}
}
public List<User> getUsers() throws HibernateException, Exception
{
try(Session session = HibernateUtil.getSessionFactory().openSession()){
return session.createQuery("FROM User", User.class).getResultList();
}
}
}
我的GeneralDAO:
import Hibernate.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
//@Transactional
@Repository
public class GeneralDAO
{
public void add(Object obj)
{
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession())
{
// start a transaction
transaction = session.beginTransaction();
// add the user object
session.save(obj);
// commit transaction
transaction.commit();
//closing session
session.close();
} catch (Exception e)
{
if (transaction != null)
{
transaction.rollback();
}
e.printStackTrace();
}
}
public void delete(int userID, Object obj)
{
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession())
{
obj = session.load(obj.getClass(), userID);
// start a transaction
transaction = session.beginTransaction();
//deleting the user from the db
session.delete(obj);
// commit transaction
transaction.commit();
//closing session
session.close();
} catch (Exception e)
{
if (transaction != null)
{
transaction.rollback();
}
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您读到的有关测试DAO的信息是正确的方法。
不要与Mockito一起测试DAO /存储库层。
您不想编写断言确实调用了语句流的单元测试:
// start a transaction
transaction = session.beginTransaction();
// add the user object
session.save(obj);
// commit transaction
transaction.commit();
//closing session
session.close();
编写这种测试意味着主要编写模拟来描述流程。
没有价值,因为它从逻辑/行为的角度检查了一切。
同样,断言查询是无用的。您可以在DAO中编写“ SELECT SELECT SELECT”作为查询,并且如果您依靠检查查询文本,则测试仍然可以成功。
您使用Spring。如果您还使用Spring Boot,则应该依靠@DataJpaTest
测试切片,该切片主要用于测试数据访问组件。
否则,不用担心。我们是在Spring Boot之前完成的。所以我们仍然可以做到。
为测试配置内存数据库(例如H2),并根据测试的方法清除/填充数据。
例如:
@Autowired
UserDAO userDAO;
@Test
public void getUser_retrieves_users_added_by_addUser(){
User addedUser = userDAO.add(new User("foo", "bar"));
// ... flush data in the database and clear first cache level to avoid cache using
User expectedUser = userDAO.get(addedUser.getId());
// assert the expected User
}
@Before
public void tearDown(){
// clear data in the database
}