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