随笔分类 -  mock

单元测试
摘要:1,引入powermock依赖 <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <version>2.0.9</version> <scope>test</scope> </d 阅读全文
posted @ 2023-06-05 15:28 ppjj 阅读(281) 评论(0) 推荐(0) 编辑
摘要:The reason is that the XML framework tries to instantiate classes using reflection and does this from the thread context classloader (PowerMock's clas 阅读全文
posted @ 2020-01-09 23:28 ppjj 阅读(2558) 评论(0) 推荐(0) 编辑
摘要:一、准备工作 1、导入测试依赖 2、Controller层: 3、UserService实现如下: 二、测试 1、创建第一个测试用例: 在类上添加@RunWith和@SpringBootTest表示是一个可以启动容器的测试类 2、Spring Test支持的一个很好的特性是应用程序上下文在测试之间缓 阅读全文
posted @ 2020-01-09 23:02 ppjj 阅读(1710) 评论(0) 推荐(1) 编辑
摘要:单元测试和集成测试的主要区别在于是否使用spring上下午ApplicationContext,如果只有@RunnerWith(SpringRunner.class)注解,则表示单元测试,而@SpringBootTest是集成测试。因为测试分片不需要上下文,所以测试分片是单元测试,有@JsonTes 阅读全文
posted @ 2020-01-09 22:57 ppjj 阅读(959) 评论(0) 推荐(0) 编辑
摘要:mock模拟private static final now=system.currentmills; 首先在测试类开头加上: @prepareForTest({System.class}) Class A{ } 然后,在方法上添加:powermockito.mockstatic(system.cl 阅读全文
posted @ 2020-01-09 22:51 ppjj 阅读(8023) 评论(0) 推荐(1) 编辑
摘要:Spring中执行单元测试,最麻烦的就是解决Bean的定义以及注入的问题。最开始使用Spring的上下文初始化进行测试,开头是这样的: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/config/Spring-db1. 阅读全文
posted @ 2020-01-09 22:44 ppjj 阅读(4770) 评论(0) 推荐(0) 编辑
摘要:一、单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期修改后(不论是增加新功能,修改bug),都可以做到重新测试的工作。以减少我们在发布的时候出现更过甚至是出现之前解决了的问题再次重现。 这里主要是使用MockMvc对我们的系统的Controller进行 阅读全文
posted @ 2020-01-09 22:25 ppjj 阅读(382) 评论(0) 推荐(0) 编辑
摘要:由于springboottest需要初始化上下文,每一次都要好长时间,可以使用其他方法替换它: 测试类如下: WebConfig.java类: 阅读全文
posted @ 2020-01-05 20:54 ppjj 阅读(2260) 评论(0) 推荐(0) 编辑
摘要:I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: I just announced the new Learn Spring course, 阅读全文
posted @ 2020-01-05 19:09 ppjj 阅读(950) 评论(0) 推荐(0) 编辑
摘要:在测试用例中,指定初始化方式 @ContextConfiguration(classes = RedisConf.class, initializers = ConfigFileApplicationContextInitializer.class) 对应的配置类 本文转自:https://www. 阅读全文
posted @ 2020-01-05 19:06 ppjj 阅读(1514) 评论(0) 推荐(0) 编辑
摘要:Mock方法内部new出来的对象 测试目标代码: 01 public class ClassUnderTest { 02 03 public boolean callInternalInstance(String path) { 04 05 File file = new File(path); 0 阅读全文
posted @ 2020-01-05 18:08 ppjj 阅读(5885) 评论(0) 推荐(0) 编辑
摘要:要模拟的类: 使用PowerMock模拟: 阅读全文
posted @ 2020-01-05 18:02 ppjj 阅读(2962) 评论(0) 推荐(0) 编辑
摘要:https://github.com/powermock/powermock/wiki/MockitoUsage#a-full-example-for-mocking-stubbing--verifying-static-method 阅读全文
posted @ 2017-03-16 14:20 ppjj 阅读(454) 评论(0) 推荐(0) 编辑
摘要:要测试的类:IndexController.java 代码: @Mock private TemplateWrapper templateWrapper = spy(new TemplateWrapper()); @InjectMocks private IndexController contro 阅读全文
posted @ 2017-03-14 14:55 ppjj 阅读(17855) 评论(0) 推荐(0) 编辑
摘要:错误原因:通过定位发现是找不到TestRule这个类,检查项目引用的Junit版本为4.7,发现TestRule是在Junit版本4.10后添加的新特性 解决方法:把junit版本由4.7改成4.10 阅读全文
posted @ 2017-03-07 19:32 ppjj 阅读(1129) 评论(0) 推荐(0) 编辑
摘要:verify(advertismentService).queryAdvitismentInfForApp(baseBOs, false); 阅读全文
posted @ 2017-03-06 20:24 ppjj 阅读(450) 评论(0) 推荐(0) 编辑
摘要:错误原因:mock的时候,不能mock重载的方法 解决方法:直接mock它的父类的方法 org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue: 'flush' is a *void method* and it *ca 阅读全文
posted @ 2017-03-03 14:58 ppjj 阅读(3037) 评论(0) 推荐(0) 编辑
摘要:@RunWith(PowerMockRunner.class)@PrepareForTest(Helper.class)//1.添加要初始化的类,就是构造函数所在的类public class HelperTest { @Mock private Something mockSomething; @I 阅读全文
posted @ 2017-03-03 11:38 ppjj 阅读(10137) 评论(0) 推荐(0) 编辑
摘要://调用void方法 doNothing().when(templateWrapper).process();// doCallRealMethod().when(templateWrapper).process(); 阅读全文
posted @ 2017-03-02 15:14 ppjj 阅读(1127) 评论(0) 推荐(0) 编辑
摘要:错误原因:invoke方法的时候,应该是类的实例对象,而不是类本身 解决方法:把 PowerMockito.doReturn(index_expect).when(IndexController.class, "processPage", pageCode, request, response, m 阅读全文
posted @ 2017-03-01 17:13 ppjj 阅读(5572) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示