模拟套餐私人课程 [英] Mocking package private classes

查看:60
本文介绍了模拟套餐私人课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Mockito或其他类似框架.有没有一种方法可以模拟包专用类? 我正在尝试模拟我的DAO类来测试我的服务层.问题在于DAO实例是包私有的,只能通过工厂获得.

Using Mockito or another similar framework. Is there a way to mock a package-private class? I am trying to test my service layer mocking my DAO classes. The problem is that the DAO instances are package private and only can be obtained through a factory.

MyPackagePrivateDao mockedDao = mock(MyPackagePrivateDao.class);

编译器说不能从包外部访问该类. 你有什么例子吗?

The compiler says that the class cannot be accessed from outside the package. Do you have any example?

谢谢

推荐答案

使用Mockito无法做到这一点,它需要修改实际类的字节码.这不是计划的功能.

This is not possible with Mockito, it requires to modify the bytecode of the actual class. This is not a planned feature.

您没有最终可以为这些DAO嘲笑的接口吗?

Don't you have interfaces that you could eventually mock for these DAOs ?

另一种选择是查看PowerMock,它非常适合处理遗留代码,即当软件设计迫使您模拟静态,私有,最终等时.

Another option is to look on PowerMock which is great to deal with legacy code, ie when the software design is forcing you to mock statics, private, final, etc.

这篇关于模拟套餐私人课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆