大兴快速网站建设公司,0基础如何快速做网站,漳州本地网,网站做推广页需要什么拓展阅读
jmockit-01-jmockit 入门使用案例
jmockit-02-概览
jmockit-03-Mocking 模拟
jmockit-04-Faking 伪造
jmockit-05-代码覆盖率
mockito-01-入门介绍
mockito-02-springaop 整合遇到的问题#xff0c;失效
jmockit 说明
jmockit 可以提供基于 mock 的测试能力…拓展阅读
jmockit-01-jmockit 入门使用案例
jmockit-02-概览
jmockit-03-Mocking 模拟
jmockit-04-Faking 伪造
jmockit-05-代码覆盖率
mockito-01-入门介绍
mockito-02-springaop 整合遇到的问题失效
jmockit 说明
jmockit 可以提供基于 mock 的测试能力。
两种方式区别
JMockit有两种测试方式一种是基于行为的一种是基于状态的测试
1. Behavior-oriented(Expectations Verifications)
其定义mock的方式就是先录制好某些方法调用和返回结果可以通过Expectations实现。 基于行为的Mock 测试一共三个阶段record、replay、verify。
1record在这个阶段各种在实际执行中期望被调用的方法都会被录制。
2repaly在这个阶段执行单元测试Case原先在record 阶段被录制的调用都可能有机会被执行到。这里有“有可能”强调了并不是录制了就一定会严格执行。
3verify在这个阶段断言测试的执行结果或者其他是否是原来期望的那样。
2. State-oriented(MockUp)
覆盖原方法的实现可以用MockUp实现 具体这两种方法如何使用会穿插在后面的不同使用场景中。
两种方式使用说明
JMockit有两种测试方式
1、基于状态的Mock
是站在目标测试代码内部的可以对传入的参数进行检查、匹配才返回某些结果类似白盒。
主要使用MockUp和Mock搭配使用实现Mock
2、基于行为的Mock
就是对Mock目标代码的行为进行模仿更像是黑盒测试。
主要使用Test、Mocked、Injectable、Capturing和Expectations搭配使用实现Mock
其实从大的方向来讲JMockit只有两种Mock方式new MockUp() 和 new Expectations() 两种。
1注解Mock是和new MockUp()方式搭配使用。
2注解Test、Mocked、Injectable、Capturing是和new Expectations()方式搭配使用。然后Mocked、Injectable、Capturing又有不同的特性就可以解决不同场景下的Mock了。
拓展阅读
junit5
使用入门
maven 引入
与 springboot 整合
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdjmockit-learn/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.1.5.RELEASE/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project继续添加 jmockit 的依赖
dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope
/dependency
dependencygroupIdorg.jmockit/groupIdartifactIdjmockit/artifactIdversion1.34/versionscopetest/scope
/dependency基于 Mockup Mock 的用法
例子
package com.github.houbb.jmockit.learn.biz;import com.github.houbb.jmockit.learn.model.UserInfo;
import com.github.houbb.jmockit.learn.service.UserService;
import mockit.*;
import mockit.integration.junit4.JMockit;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;RunWith(JMockit.class)
public class UserBizMockUpCaseTest {// 待测试的实现需要指定为具体的实现Testedprivate UserBiz userBiz;// 依赖的属性进行 mockInjectableprivate UserService userService;Testpublic void test() {//mocknew MockUpUserService(userService) {Mockpublic UserInfo queryById(String id) {UserInfo user new UserInfo();user.setId(id);user.setName(id-name-mock);return user;}};UserInfo userInfo userBiz.queryUserInfo(2);Assert.assertEquals(2-name-mock, userInfo.getName());}}基于状态的测试
{% raw %}
package com.github.houbb.jmockit.learn.biz;import com.github.houbb.jmockit.learn.model.UserInfo;
import com.github.houbb.jmockit.learn.service.UserService;
import com.github.houbb.jmockit.learn.service.impl.UserServiceImpl;
import mockit.*;
import mockit.integration.junit4.JMockit;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;RunWith(JMockit.class)
public class UserBizMockedTest {// 依赖的属性进行 mockMockedprivate UserServiceImpl userService;Testpublic void test2() {// 录制(Record)new Expectations() {{userService.queryById((String) any); result new UserInfo(any-other, any-other-name);}};//重放(Replay)UserInfo userInfo1 userService.queryById(1);UserInfo userInfo2 userService.queryById(2);UserInfo userInfo3 userService.queryById(3);// 也可以断言Assert.assertTrue(userInfo1.getName().equals(any-other-name));// 验证验证被调用且被调用了3次new Verifications() {{userService.queryById((String) any);times 3;}};}}
{% endraw %}