Comments (6)
PrivateAccessor.set注入以后,如果被测的方法里用了set进去的对象,@MockMethod
是可以在测试的时候把调用替换成Mock方法的。比如set进去的对象是BComponent
,接下来可以用@MockMethod(targetClass = BComponent.class)
去换要调用的方法。
需要澄清的一件事是单元测试一般不推荐跨类进行Mock。在比较规范的单元测试里,AServiceTest
类应该只做AService
代码逻辑的验证。比如上面的例子,把一个BComponent
类型对象bComponent
放到被测的AService
对象里以后,可以把不想执行的bComponent
对象方法调用Mock掉。但在BComponent
类型的代码里再调用CComponent
的逻辑应该在BComponentTest
测试类去做,因此AServiceTest
里用的bComponent
对象里的cComponent
为空对这个测试类应该不影响。
如果确实要做跨类(跨单元)的单元测试,其实也能实现,需要在BComponent
的Mock类里写对cComponent
对象的Mock,但在目前的TestableMock版本里这样写测试会导致可读性下降比较多,正在重构中的0.5
版本对这种场景的支持会更好一些。
from testable-mock.
如果Facade里的接口不太多,可以new一个匿名类,比如:
PrivateAccessor.set(collectBiz, "collectRouterFacade", new CollectRouterFacade() {
... // 这里覆写接口方法,可以根据测试需要来实现
});
用IDE辅助可以批量补齐需要覆写的方法
from testable-mock.
对于Mabatis的Mapper接口也是类似的处理方式吗?有没有其他方式
from testable-mock.
这样写有点复杂啊,尤其是接口比较多的类,这样实现起来虽然idea可以自动生成,但是代码一大堆
from testable-mock.
@lithStudy 确实比较大的临时类,可以在测试目录里单建文件来放,这样即不会让测试类变得很乱,又能在需要的时候进行复用
from testable-mock.
0.5.0版本已发布🚀
from testable-mock.
Related Issues (20)
- java.lang.NoSuchMethodError:Mock容器类下的@MockNew方法,无法通过@MockContainer继承生效
- OmniConstructor工具类,开启了字节码增强,但mock对象时还是会随机偶发报错 HOT 3
- when use MockWith annotation, mockClass not right HOT 2
- 包路径映射后,mock未生效
- Maven package failed due to EnablePrivateAccessProcessor HOT 5
- 建议:建议增加两个快速开始文档 HOT 1
- OmniConstructor.newInstance(RyNormalTask.class) 报错 Failed to invoke constructor HOT 1
- 如何避免mock代码异常提交 HOT 3
- @MockContainer 注解对于继承类的void方法不能正常生效
- 被测试类的方法没有被mock HOT 1
- 使用@RequiredArgsConstructor出现Failed to load ApplicationContext
- 与Jacoco一起使用时,不支持Java17
- 使用OmniConstructor.newInstance()报错 HOT 1
- 从0.7.7升级到0.7.9,启用OmniConstructor的字节码增强模式效率变得非常慢
- 按文档在test里设置jvmArgs报空指针
- 测试service 中调用dao 中继承自mybatis中basemapper通用方法,mock dao层 不生效
- dao层mock 新增 修改 返回int类型接口,启动test报错,类型不匹配,改成Integer,mock不生效
- SpringBoot3中@{argLine} 和 -javaagent不能同时使用
- springboot框架下拦截器中的方法怎么修改
- 一个被测试类可以有个多个Mock类吗?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testable-mock.