Java 用Mockito对DAO层进行单元测试
A尝试测试我的DAO方法:Java 用Mockito对DAO层进行单元测试,java,spring,unit-testing,mockito,dao,Java,Spring,Unit Testing,Mockito,Dao,A尝试测试我的DAO方法: @Repository @ComponentScan(basePackages = {"com.sebhaw.cms.entity"}) public class AuthoritiesDAO extends CommonClass implements IDAO<Authorities>{ public final Logger logger = LoggerFactory.getLogger(AuthoritiesDAO.
@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();
}
}
@存储库
@ComponentScan(basePackages={“com.sebhaw.cms.entity”})
公共类权威DAO扩展CommonClass实现IDAO{
public final Logger Logger=LoggerFactory.getLogger(authorities dao.class);
公共实体管理者实体管理者;
@自动连线
公共机构DAO(EntityManager EntityManager){
this.entityManager=entityManager;
}
私人普通舱;
静态最终日期TimeFormatter DateTimeFormatter=DateTimeFormatter.of模式(“HH:mm:ss”);
@凌驾
公共列表getObjects(){
会话currentSession=entityManager.unwrap(会话.class);
Query theQuery=currentSession.createQuery(“来自Authorities”,Authorities.class);
List authorities=theQuery.getResultList();
logger.warn(“+dateTimeFormatter.format(LocalDateTime.now())+”中的“+getPrincipal().toString()”)中查询表权限的GetAuthories()”;
返回当局;
}
@凌驾
公共无效保存对象(t){
会话currentSession=entityManager.unwrap(会话.class);
t、 setLastModifier(getPrincipal());
logger.warn(“在日内保存在表权限上的记录:“+dateTimeFormatter.format(LocalDateTime.now())+”,数据:“+t.toString()+”由:“+getPrincipal().toString())”;
currentSession.saveOrUpdate(t);
}
@凌驾
公共机构获取对象(int theId){
会话currentSession=entityManager.unwrap(会话.class);
Authorities theAuthorities=currentSession.get(Authorities.class,theId);
logger.warn(“查询日内表权限的GetAuthories(int theId):”
+dateTimeFormatter.format(LocalDateTime.now())+,ID为:“+theId+”by:“+getPrincipal().toString());
交还权限;
}
@凌驾
公共无效删除实体(int theId){
会话currentSession=entityManager.unwrap(会话.class);
Query theQuery=currentSession.createQuery(“从权限中删除,其中id=:authoritiesId”);
setParameter(“authorities id”,theId);
logger.warn(“在日期:“+dateTimeFormatter.format(LocalDateTime.now())+”,ID为:“+theId+”的记录被删除于:“+getPrincipal().toString()”);
executeUpdate();
}
}
这是我的单元测试(不工作):
公共类权威DAOTEST{
@嘲弄
非公开会议;
私人实体管理者实体管理者;
私人普通舱;
私人权威道权威道;
@之前
void init(){
entityManager=Mockito.mock(entityManager.class);
Mockito.when(entityManager.unwrap(Session.class)).thenReturn(Session);
authoritiesDAO=新的authoritiesDAO(entityManager);
}
@试验
void应返回EmptyCollection(){
Query=Mockito.mock(Query.class);
Mockito.when(session.createQuery(ArgumentMatchers.anyString(),ArgumentMatchers.any())。然后返回(查询);
Mockito.when(query.getResultList()).thenReturn(newArrayList());
列表结果=authoritiesDAO.getObjects();
资产真实(真实);
}
}
我要求提供一个对我的dao层进行有效单元测试的示例,因为我处于死胡同中。 “如果你是我的朋友,那你就是我的朋友。” “如果你是我的朋友,那你就是我的朋友。” “如果你是我的朋友,那你就是我的朋友。”
“你可以自己用谷歌搜索它,你会得到多个链接。”。部分链接
https://dzone.com/articles/unit-testing-dao-service-and-controller-in-spring
https://howtodoinjava.com/best-practices/how-you-should-unit-test-dao-layer/
“不工作”不是一个有用的描述。请提供错误消息和堆栈跟踪(如果有),或以其他方式明确解释在这种情况下“不工作”的含义(即您正在经历的行为以及它与您预期的不同)。这样的话,你就不需要在你的问题上附加部分同侧性知识。“问题”。它的措辞非常像现在的“给我代码”,你可能也想编辑它。阅读一些提示。我的错误。我在第46行有NullPointerException:
Mockito.when(session.createQuery(ArgumentMatchers.anyString(),ArgumentMatchers.any())。然后返回(query)代码>将其包括在问题中。你认为我是在寻求谷歌指南还是以代码的形式寻求帮助,因为我已经从指南中用尽了所有可能的选项?“我在问一个关于我的dao层的有效单元测试的例子,因为我正处于死胡同中。”这意味着什么?
https://dzone.com/articles/unit-testing-dao-service-and-controller-in-spring
https://howtodoinjava.com/best-practices/how-you-should-unit-test-dao-layer/