测试用例中Spring+mock的值代替不确定值
来源:互联网 发布:绝地求生渣优化 编辑:程序博客网 时间:2024/06/16 12:36
1.首先在maven里面配置jar
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito --><dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.7.1</version> <scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 --><dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.7.1</version> <scope>test</scope></dependency>
2.在类里面使用注解,如果需要mock和spring一起用需要以下内容:
@RunWith(PowerMockRunner.class) //使用PowerMockRunner运行时 @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) //委派给SpringJUnit4ClassRunner @PrepareForTest(VelocityFormatUtils.class)@PowerMockIgnore({"javax.management.*"}) //忽略一些mock异常
3.代码示例:
package com.xiaomar.bis.template;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.powermock.api.mockito.PowerMockito;import org.powermock.core.classloader.annotations.PowerMockIgnore;import org.powermock.core.classloader.annotations.PrepareForTest;import org.powermock.modules.junit4.PowerMockRunner;import org.powermock.modules.junit4.PowerMockRunnerDelegate;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.alibaba.fastjson.JSONObject;import com.xiaomar.bis.utils.VelocityFormatUtils;import com.xiaomar.bis.utils.VelocityTestUtils;import com.xiaomar.bis.utils.VmTestConstants;@RunWith(PowerMockRunner.class) //使用PowerMockRunner运行时 @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) //委派给SpringJUnit4ClassRunner @PrepareForTest(VelocityFormatUtils.class)@PowerMockIgnore({"javax.management.*"}) //忽略一些mock异常public class TestFxHealthConfirmProIn { private static final String CURRENT_TPL_NAME = "fxHealthConfirmProIn.vm"; private static final String SERIALNO = "serialNo"; private static final String INSURED_TOTALPREMIUM = "insured.totalPremium"; private static final String APPLICATION_TIME = "application.time"; private static final String PREMIUM_PAYYEARS = "premium.payYears"; private static final String PERIOD = "period"; private static final String INSURED_RELATIONSHIPWITHAPPLICANT = "insured.relationshipWithApplicant"; private static final String BENEFICIARY_NAME = "beneficiary.name"; private static final String YPREM = "yPrem"; private static final String PREMIUM_SECURITYLINES= "premium.securityLines"; private static final String MEDICAL_BUYCOPIES = "medical.buyCopies"; private static final String INSURED_INSUREDAMOUNT = "insured.insuredAmount"; private static final String NPREM = "nPrem"; private static final String APPLICATION_NAME = "application.name"; private static final String APPLICATION_SEX = "application.sex"; private static final String APPLICATION_BIRTHDAY = "application.birthday"; private static final String APPLICATION_IDTYPE = "application.idType"; private static final String APPLICATION_IDNO = "application.idno"; private static final String APPLICATION_VALIDATE = "application.validate"; private static final String APPLICATION_OCCUPATION = "application.occupation"; private static final String APPLICATION_MOBILE = "application.mobile"; private static final String APPLICATION_EMAIL = "application.email"; private static final String APPLICATION_ADDRNO = "application.addrNo"; private static final String APPLICATION_CURRENTRESIDENCE = "application.currentResidence"; private static final String PROVINCE = "province"; private static final String CITY = "city"; private static final String COUNTY = "county"; private static final String APPLICATION_DETAILEDADDRESS = "application.detailedAddress"; private static final String APPLICATION_POSTALCODE = "application.postalCode"; private static final String INSURED_NAME = "insured.name"; private static final String INSURED_SEX = "insured.sex"; private static final String INSURED_BIRTHDAY = "insured.birthday"; private static final String INSURED_IDTYPE = "insured.idType"; private static final String INSURED_IDNO = "insured.idno"; private static final String PROINFO_OCCCATEGORY = "proInfo.occCategory"; private static final String INSURED_MOBILE = "insured.mobile"; private static final String INSURED_EMAIL = "insured.email"; private static final String INSURED_HEIGHT = "insured.height"; private static final String INSURED_WEIGHT = "insured.weight"; private static final String INSURED_CURRENTRESIDENCE = "insured.currentResidence"; private static final String INSURED_DETAILEDADDRESS = "insured.detailedAddress"; private static final String INSURED_POSTALCODE = "insured.postalCode"; @Before public void beforeTest(){ PowerMockito.mockStatic(VelocityFormatUtils.class); PowerMockito.when(VelocityFormatUtils.getDateFormatOne()).thenReturn("2017-10-23 14:48:27"); JSONObject addressJson = new JSONObject(); addressJson.put(PROVINCE, "110000"); addressJson.put(CITY, "110100"); addressJson.put(COUNTY, "110101"); PowerMockito.when(VelocityFormatUtils.stringToJSON(addressJson.toJSONString())).thenReturn(addressJson); } @Test public void testRegular() throws IOException { Map<String, String> tempMap = initTempMap("XM01","0"); Map<String, Object> inDataMap = initInDataMap("551AF554539947FC8A08654504EDE07E","105", "539.50"); stubExector(tempMap, inDataMap, ""); } @Test public void testRegularHasNull() throws IOException { Map<String, String> tempMap = initTempMap(null,"0"); Map<String, Object> inDataMap = initInDataMap(null,"105", "539.50"); stubExector(tempMap, inDataMap, "HasNull"); } @Test public void testHasEmpty() throws IOException { Map<String, String> tempMap = initTempMap("","0"); Map<String, Object> inDataMap = initInDataMap("", "", "963200"); stubExector(tempMap, inDataMap, "HasEmpty"); } private void stubExector(Map<String, String> tempMap, Map<String, Object> inDataMap, String suffix) throws IOException { String answerPath = VelocityTestUtils.combinePath(CURRENT_TPL_NAME, suffix); Map<String, Object> dataMap = new HashMap<String, Object>(); Map<String, String> fileInfo = new HashMap<>(); fileInfo.put(VmTestConstants.ANSWER_PATH, answerPath); fileInfo.put(VmTestConstants.TPL_NAME, CURRENT_TPL_NAME); VelocityTestUtils.testVelocity(dataMap, tempMap, inDataMap, fileInfo); } private Map<String, String> initTempMap(String extenterpcode,String asyn){ Map<String, String> tempMap = new HashMap<>(); tempMap.put("extenterpcode",extenterpcode); tempMap.put("asyn",asyn); tempMap.put("thirdUrl",""); return tempMap; } private Map<String, Object> initInDataMap(String serialNo, String period, String yPrem){ Map<String, Object> map = new HashMap<>(); map.put(SERIALNO,serialNo); map.put(INSURED_TOTALPREMIUM,"569.30"); map.put(APPLICATION_TIME,"2017-10-18 17:01:26"); map.put(PREMIUM_PAYYEARS,"20"); map.put(PERIOD,period); map.put(INSURED_RELATIONSHIPWITHAPPLICANT,"00"); map.put(BENEFICIARY_NAME,"Y"); map.put(YPREM,yPrem); map.put(PREMIUM_SECURITYLINES,"50000"); map.put(MEDICAL_BUYCOPIES, "1"); map.put(NPREM, "29.80"); map.put(APPLICATION_NAME, "王丽萍"); map.put(APPLICATION_SEX, "1"); map.put(APPLICATION_BIRTHDAY, "1989-10-12"); map.put(APPLICATION_IDTYPE, "A"); map.put(APPLICATION_TIME, "2017-10-18 17:01:26"); map.put(APPLICATION_IDNO, "G23473216"); map.put(APPLICATION_VALIDATE, ""); map.put(APPLICATION_OCCUPATION, ""); map.put(APPLICATION_MOBILE, "18817315256"); map.put(APPLICATION_EMAIL, "xbkfdjk@qq.com"); map.put(APPLICATION_ADDRNO, ""); JSONObject addressJson = new JSONObject(); addressJson.put(PROVINCE, "110000"); addressJson.put(CITY, "110100"); addressJson.put(COUNTY, "110101"); map.put(APPLICATION_CURRENTRESIDENCE, addressJson.toJSONString()); map.put(APPLICATION_DETAILEDADDRESS, "小胡同口"); map.put(APPLICATION_POSTALCODE, "342300"); map.put(INSURED_NAME, "丽泽"); map.put(INSURED_SEX, "1"); map.put(INSURED_BIRTHDAY, "2000-10-20"); map.put(INSURED_IDTYPE, "A"); map.put(INSURED_IDNO, "G20134564"); map.put(PROINFO_OCCCATEGORY, "000101"); map.put(INSURED_MOBILE, "18817315256"); map.put(INSURED_EMAIL, ""); map.put(INSURED_HEIGHT, "173"); map.put(INSURED_WEIGHT, "70"); JSONObject insuredJson = new JSONObject(); insuredJson.put(PROVINCE, "110000"); insuredJson.put(CITY, "110100"); insuredJson.put(COUNTY, "110101"); map.put(INSURED_CURRENTRESIDENCE, insuredJson.toJSONString()); map.put(INSURED_DETAILEDADDRESS, ""); map.put(INSURED_INSUREDAMOUNT, "0.01"); map.put(INSURED_POSTALCODE, ""); return map; }}
阅读全文
0 0
- 测试用例中Spring+mock的值代替不确定值
- mock测试spring boot的CRUD服务
- 使用Spring mock测试
- Spring MVC MOCK测试
- Spring 调用mock 测试
- SQLServer2000 SUM 的值不确定?
- Spring MVC的单元测试和集成测试(不使用mock)
- Spring MVC的单元测试和集成测试(不使用mock)
- 解析key值不确定的json数据
- 使用spring-mock进行dao集成测试
- 使用Spring Mock测试Action中的方法
- Spring自带mock测试Controller
- 利用Mock测试Spring中Databinder
- Spring自带mock测试Controller
- Spring自带mock测试Controller
- Spring mock测试 回滚数据库
- JUNIT+SPRINGMVC的mock测试
- java的mock测试框架
- Spring 中 @Service 和 @Resource 注解的区别
- Tips-右键菜单清除选项
- CodeForces
- 如何写代码,编程内功心法
- hadoop 常用命令
- 测试用例中Spring+mock的值代替不确定值
- 快速计算掩码位数
- PowerDesigner使用指南
- 字符串常量到底存在哪了?
- Java Logging
- poj2431(优先队列)
- git 生成patch
- github上关于iOS的各种开源项目集合(转)
- hexdump.asm