用mockito对dao层进行java单元测试

b5lpy0ml  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(499)

尝试测试我的dao方法:

@Repository
@ComponentScan(basePackages = {"com.sebhaw.cms.entity"})
public class AuthoritiesDAO extends CommonClass implements IDAO<Authorities>{

    public final Logger logger = LoggerFactory.getLogger(AuthoritiesDAO.class);

    public EntityManager entityManager;

    @Autowired
    public AuthoritiesDAO(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    private CommonClass commonClass;

    static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Override
    public List<Authorities> getObjects() {

        Session currentSession = entityManager.unwrap(Session.class);
        Query<Authorities> theQuery = currentSession.createQuery("from Authorities",Authorities.class);
        List<Authorities> authorities = theQuery.getResultList();
        logger.warn("Query getAuthorities() on table Authorities in day: "+dateTimeFormatter.format(LocalDateTime.now())+" by: "+getPrincipal().toString());
        return authorities;
    }

    @Override
    public void saveObjects(Authorities t) {

        Session currentSession = entityManager.unwrap(Session.class);
        t.setLastModifier(getPrincipal());
        logger.warn("Record saved on table Authorities in day: "+dateTimeFormatter.format(LocalDateTime.now())+", data: "+t.toString()+" by: "+getPrincipal().toString());
        currentSession.saveOrUpdate(t);
    }

    @Override
    public Authorities getObjects(int theId) {
        Session currentSession = entityManager.unwrap(Session.class);
        Authorities theAuthorities = currentSession.get(Authorities.class, theId);
        logger.warn("Query getAuthorities(int theId) on table Authorities in day: "
        +dateTimeFormatter.format(LocalDateTime.now())+"with ID: "+theId+" by: "+getPrincipal().toString());
        return theAuthorities;
    }

    @Override
    public void deleteEntities(int theId) {

        Session currentSession = entityManager.unwrap(Session.class);
        Query theQuery = currentSession.createQuery("delete from Authorities where id=:authoritiesId");
        theQuery.setParameter("authoritiesId", theId);
        logger.warn("Record deleted in table Authorities on day: "+dateTimeFormatter.format(LocalDateTime.now())+", with ID: "+theId+" by: "+getPrincipal().toString());
        theQuery.executeUpdate();   
    }
}

这是我的单元测试(不起作用):

public class AuthoritiesDAOTest {

    @Mock
    private Session session;

    private EntityManager entityManager;

    private CommonClass commonClass;

    private AuthoritiesDAO authoritiesDAO;

    @BeforeEach
    void init() {
        entityManager = Mockito.mock(EntityManager.class);
        Mockito.when(entityManager.unwrap(Session.class)).thenReturn(session);
        authoritiesDAO = new AuthoritiesDAO(entityManager);
    }

    @Test
    void shouldReturnEmptyCollection() {
        Query query = Mockito.mock(Query.class);
        Mockito.when(session.createQuery(ArgumentMatchers.anyString(),ArgumentMatchers.any())).thenReturn(query);
        Mockito.when(query.getResultList()).thenReturn(new ArrayList<Authorities>());

        List<Authorities> result = authoritiesDAO.getObjects();
        assertTrue(true);
    }

}

我要求提供一个对我的dao层进行有效单元测试的示例,因为我正处于死胡同。”我是说:“我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,阿迪皮西·维利特……“我是说,我是说,我是说,我是说,阿迪皮西·维利特……”

ffx8fchx

ffx8fchx1#

你可以自己用谷歌搜索它会得到多个链接。一些链接

https://dzone.com/articles/unit-testing-dao-service-and-controller-in-spring
https://howtodoinjava.com/best-practices/how-you-should-unit-test-dao-layer/

相关问题