一、环境准备:

jdk1.7+,Junit4.9,Spring3.2.0。

二、测试代码部分:

  • DataSynchReceiveServiceImpl.java
package com.snt.aaa.config.service.impl;import java.util.List;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snt.aaa.common.core.service.BaseService;
import com.snt.aaa.common.entity.BaseEntity;
import com.snt.aaa.common.utils.StringUtil;
import com.snt.aaa.config.intf.entity.AccountDuration;
import com.snt.aaa.config.intf.entity.AccountInfo;
import com.snt.aaa.config.intf.entity.AccountTraffic;
import com.snt.aaa.config.intf.entity.CustomerInfo;
import com.snt.aaa.config.intf.entity.SubscriberInfo;
import com.snt.aaa.config.intf.entity.SubscriberSubscription;
import com.snt.aaa.config.intf.entity.SubscriptionInfo;
import com.snt.aaa.config.mapper.SubscriberInfoMapper;
import com.snt.aaa.config.service.AccountDurationService;
import com.snt.aaa.config.service.AccountInfoService;
import com.snt.aaa.config.service.AccountTrafficService;
import com.snt.aaa.config.service.CustomerInfoService;
import com.snt.aaa.config.service.DataSynchReceiveService;
import com.snt.aaa.config.service.ProductFeeRuleService;
import com.snt.aaa.config.service.ProductInfoService;
import com.snt.aaa.config.service.ProductParameterService;
import com.snt.aaa.config.service.ProductServiceService;
import com.snt.aaa.config.service.ServiceInfoService;
import com.snt.aaa.config.service.SubscriberInfoService;
import com.snt.aaa.config.service.SubscriberSubscriptionService;
import com.snt.aaa.config.service.SubscriptionInfoService;@Service("dateSynchReceiveService")
public class DataSynchReceiveServiceImpl implements DataSynchReceiveService{public static  final Logger log = LoggerFactory.getLogger(DataSynchReceiveServiceImpl.class);@Autowiredprivate CustomerInfoService customerInfoService;@Autowiredprivate SubscriberInfoService subscriberInfoService;@Autowiredprivate SubscriberSubscriptionService sss;@Autowiredprivate AccountTrafficService accountTrafficService;@Autowiredprivate AccountDurationService accountDurationService;@Autowiredprivate SubscriptionInfoService subscriptionInfoService;@Autowiredprivate ProductInfoService productInfoService;@Autowiredprivate ProductFeeRuleService productFeeRuleService;@Autowiredprivate ProductParameterService productParameterService;@Autowiredprivate ProductServiceService productServiceService;@Autowiredprivate ServiceInfoService serviceInfoService;@Autowiredprivate AccountInfoService accountInfoService;@Autowiredprivate SubscriberInfoMapper sim;public void saveReceiveData(JSONObject resultJson){try {JSONObject message = resultJson.getJSONObject("message");//获取客户信息JSONObject cObj = message.getJSONObject("customer");if(cObj!=null){CustomerInfo cinfo = JSON.toJavaObject(cObj, CustomerInfo.class);customerInfoService.insert(cinfo); }//获取用户信息JSONArray subscribers = message.getJSONArray("subscriber");saveEntity(subscribers, SubscriberInfo.class, subscriberInfoService); //获取订购关系JSONArray subscriptions = message.getJSONArray("subscriptions");saveEntity(subscriptions, SubscriptionInfo.class, subscriptionInfoService); //获取流量账户JSONArray subTraffics = message.getJSONArray("subTraffics");saveEntity(subTraffics, AccountTraffic.class, accountTrafficService); //新建时长账户json 数组JSONArray subDuration = message.getJSONArray("subDuration");saveEntity(subDuration, AccountDuration.class, accountDurationService); //获取订购关系中间表记录JSONArray subSubscriptions =message.getJSONArray("subSubscription");saveEntity(subSubscriptions, SubscriberSubscription.class, sss); //获取用户账户信息JSONArray accounts =message.getJSONArray("accounts") ;saveEntity(accounts, AccountInfo.class, accountInfoService);//二、解析message中的数据//1.获取所有t_product_info表中的数据JSONArray products = message.getJSONArray("products");//2.获取所有t_product_service表中的数据JSONArray productService = message.getJSONArray("productService");//3.获取所有t_service_info表中的所有数据JSONArray sevices = message.getJSONArray("sevices");//4.获取所有t_product_parameter表中的数据JSONArray productParas = message.getJSONArray("productParas");//5.获取所有t_product_fee_rule表中的数据JSONArray productsFee = message.getJSONArray("productsFee");//三、执行保存动作saveProducts(products);saveProductService(productService);saveSevices(sevices);saveProductParas(productParas);saveProductsFee(productsFee);} catch (Exception e) {log.error("crm 数据 保存数据到3a 中出错",e);}}public void deleteCustomerRelate(String customerId){if(StringUtil.isNotNull(customerId)){List<String> ids = subscriptionInfoService.getIdsByCustomerId(customerId);for(String id:ids){//1.删除订购关系sss.deleteBySubscriptionId(id);//2.删除流量账户accountTrafficService.deleteBySubscriptionId(id);//3.删除时长账户信息accountDurationService.deleteBySubscriptionId(id);//4.删除订购关系subscriptionInfoService.delete(id);}//5.删除客户账户信息accountInfoService.deleteByCustomerId(customerId);//6.删除客户下的用户信息subscriberInfoService.deleteByCustomerId(customerId);//7.删除客户信息customerInfoService.delete(customerId);}}public void saveProducts(JSONArray products){productInfoService.saveProducts(products);}public void saveProductService(JSONArray productService){productServiceService.saveProductService(productService);}public void saveSevices(JSONArray sevices){serviceInfoService.saveSevices(sevices);}public void saveProductParas(JSONArray productParas){productParameterService.saveProductParas(productParas);}public void saveProductsFee(JSONArray productsFee){productFeeRuleService.saveProductsFee(productsFee);}@SuppressWarnings({ "unchecked", "rawtypes" })public void saveEntity(JSONArray jArray,Class<? extends BaseEntity> clz, BaseService bservice){if(jArray!=null && jArray.size()>0){for(int i=0;i<jArray.size();i++){JSONObject iObj = jArray.getJSONObject(i);bservice.insert(JSON.toJavaObject(iObj, clz));}}}}
  • DataSynchReceiveServiceImplTest.java
package com.snt.aaa.config.service.impl;import junit.framework.Assert;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snt.aaa.config.service.ProductFeeRuleService;
import com.snt.aaa.config.service.ProductInfoService;
import com.snt.aaa.config.service.ProductParameterService;
import com.snt.aaa.config.service.ProductServiceService;
import com.snt.aaa.config.service.ServiceInfoService;
import com.snt.aaa.config.service.util.TestBase;/*** @描述:测试数据同步到AAA的* @版本:v1.0*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/springmvc.xml"})
public class DataSynchReceiveServiceImplTest extends TestBase{@Autowiredprivate ProductInfoService productInfoService;@Autowiredprivate ProductServiceService productServiceService;@Autowiredprivate ServiceInfoService serviceInfoService;@Autowiredprivate ProductParameterService productParameterService;@Autowiredprivate ProductFeeRuleService productFeeRuleService;/*** 测试产品信息的保存* @param * @return*/@Testpublic void testSaveProducts(){//1.构造数据JSONArray products = new JSONArray();JSONObject product1 = new JSONObject();product1.put("id", "2016121209200200399");product1.put("productCode", "P_15");product1.put("productState", "0");product1.put("isMaster", "1");product1.put("isCancel", "1");product1.put("isRepeat", "0");JSONObject product2 = new JSONObject();product2.put("id", "2016121209200200400");product2.put("productCode", "P_16");product2.put("productState", "0");product2.put("isMaster", "1");product2.put("isCancel", "1");product2.put("isRepeat", "0");products.add(product1);products.add(product2);//2.执行方法productInfoService.saveProducts(products);Assert.assertNotNull("测试成功!"); }/*** 测试保存产品服务关系表数据的保存* @param * @return*/@Testpublic void testSaveProductService(){//1.构造数据JSONArray productServices = new JSONArray();JSONObject productService1 = new JSONObject();productService1.put("id", "1001");productService1.put("productId", "2001");productService1.put("serviceId", "3001");JSONObject productService2 = new JSONObject();productService2.put("id", "1002");productService2.put("productId", "2002");productService2.put("serviceId", "3002");productServices.add(productService1);productServices.add(productService2);//2.执行方法productServiceService.saveProductService(productServices);Assert.assertNotNull("测试成功!");}/*** 测试保存服务信息的方法* @param * @return*/@Testpublic void testSaveSevices(){//1.构造数据JSONArray serviceInfos = new JSONArray();JSONObject serviceInfo1 = new JSONObject();serviceInfo1.put("id", "1001");serviceInfo1.put("serviceCode", "P_service");serviceInfo1.put("serviceCatagory", "Data");serviceInfo1.put("serviceState", "0");JSONObject serviceInfo2 = new JSONObject();serviceInfo2.put("id", "2001");serviceInfo2.put("serviceCode", "m_service");serviceInfo2.put("serviceCatagory", "MNT");serviceInfo2.put("serviceState", "0");serviceInfos.add(serviceInfo1);serviceInfos.add(serviceInfo2);//2.执行方法serviceInfoService.saveSevices(serviceInfos);Assert.assertNotNull("测试成功!");}/*** 测试产品参数的保存* @param * @return*/@Testpublic void testSaveProductParas(){//1.构造数据JSONArray productParameters = new JSONArray();JSONObject productParameter1 = new JSONObject();productParameter1.put("id", "2016121209200200030");productParameter1.put("productId", "2016121209200200394");productParameter1.put("parameterId", "2016121209165300010");productParameter1.put("parameterValue", "是");JSONObject productParameter2 = new JSONObject();productParameter2.put("id", "2016121209232200062");productParameter2.put("productId", "2016121209232200517");productParameter2.put("parameterId", "2016121209165300010");productParameter2.put("parameterValue", "是");productParameters.add(productParameter1);productParameters.add(productParameter2);//2.执行方法productParameterService.saveProductParas(productParameters);Assert.assertNotNull("测试成功!");}/*** 测试产品资费接口数据的保存* @param * @return*/@Testpublic void testSaveProductsFee(){//1.构造数据JSONArray productFeeRules = new JSONArray();JSONObject productFeeRule1 = new JSONObject();productFeeRule1.put("id", "2016121209200200023");productFeeRule1.put("productId", "2016121209200200394");productFeeRule1.put("feeCode", "BasicPrice");productFeeRule1.put("feeValue", "20");JSONObject productFeeRule2 = new JSONObject();productFeeRule2.put("id", "2016121209200200024");productFeeRule2.put("productId", "2016121209200200394");productFeeRule2.put("feeCode", "Amount");productFeeRule2.put("feeValue", "100");productFeeRules.add(productFeeRule1);productFeeRules.add(productFeeRule2);//2.执行方法productFeeRuleService.saveProductsFee(productFeeRules);Assert.assertNotNull("测试成功!");}
}

  • TestBase.java
package com.snt.aaa.config.service.util;import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @描述:测试用例父类* @版本:v1.0*/
public class TestBase {public ClassPathXmlApplicationContext context;@Beforepublic void preTs() throws Exception {context = new ClassPathXmlApplicationContext("classpath:spring/springmvc.xml");}
}

三、测试心得:

对于运行测试代码,不需要启动任何服务,但是特别需要注意的是,一定要有正确的配置文件路径,否则测试程序很容易运行失败。
学习Java 的同学注意了!!! 
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!

如何编写Junit测试代码相关推荐

  1. 编写junit 测试_编写JUnit测试的另一种方法(Jasmine方法)

    编写junit 测试 最近,我为一个小型个人项目编写了很多Jasmine测试. 我花了一些时间才终于感到正确地完成了测试. 在此之后,当切换回JUnit测试时,我总是很难过. 由于某种原因,JUnit ...

  2. 编写JUnit测试的另一种方法(Jasmine方法)

    最近,我为一个小型个人项目编写了很多Jasmine测试. 我花了一些时间才终于感到正确地完成了测试. 此后,当我切换回JUnit测试时,我总是很难过. 出于某种原因,JUnit测试不再那么好,我想知道 ...

  3. 编写junit 测试_使用JUnit和Repeat注​​释编写有效的负载测试

    编写junit 测试 EasyTest最近推出了一组新的注释,可帮助其用户编写有效的测试用例. 进入EasyTest的两个主要注释是: 重复 持续时间 今天,我们将讨论重复标注. 一种新的方法级别注释 ...

  4. 单元测试——编写JUnit测试

    什么是单元测试呢?单元测试就是针对最小的功能单元编写测试代码.Java程序最小的功能单元是方法,因此,对Java程序进行单元测试就是针对单个Java方法的测试. 单元测试有什么好处呢?在学习单元测试前 ...

  5. [Java] 使用Android Studio编写Java测试代码

    目录 一.创建"Java or Kotlin Library" 模组. 1.1 新建Android Project. 1.2 新建 "Java or Kotlin Lib ...

  6. 在Eclipse中使用Junit测试代码

    一.在项目中加入Junit库 右击项目文件,在选项中选择BuildPath这一选项. 选择Add Libbraries选项,在其中找到Junit,可以自行选择需要的Junit版本. 当你的项目中除了之 ...

  7. 单元测试:如何编写可测试的代码及其重要性

    原文来自互联网,由长沙DotNET技术社区编译.如译文侵犯您的署名权或版权,请联系小编,小编将在24小时内删除.限于译者的能力有限,个别语句翻译略显生硬,还请见谅. 作者:谢尔盖·科洛迪(SERGEY ...

  8. 新书《编写可测试的JavaScript代码 》出版,感谢支持

    本书介绍 JavaScript专业开发人员必须具备的一个技能是能够编写可测试的代码.不管是创建新应用程序,还是重写遗留代码,本书都将向你展示如何为客户端和服务器编写和维护可测试的JavaScript代 ...

  9. apachejmeter_java源码_自定义编写jmeter的Java测试代码

    我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","JavaV ...

最新文章

  1. php 爬虫_Scrapy 爬虫完整案例-基础篇
  2. cuda-convnet2与caffe对比
  3. TeeChart.Direct2D.dll的使用
  4. C# 2018.9.17
  5. 十六个 HTML,CSS,jQuery,WordPress等快速启动项目样板
  6. 使用 System.Net.Http.Json 简化 HttpClient 的使用
  7. 有关malloc的一个小点
  8. python的类里的属性是否可以为列表_Python中如何获取类属性的列表
  9. qt客户端打包_悬赏问答 - QT 类似QQ的 服务器与客户端程序 程序打包+数据库问题...
  10. Windows Server 2003 安全指南
  11. 串口转发工具 串口屏调试神器 PC串口监视神器
  12. mac airdrop 隔空投送 我可以发现别人,别人发现不了我。搜索不到。
  13. PPT太大怎么进行压缩
  14. 随机森林+python代码实现
  15. 树莓派3b+开启无线wifi热点
  16. 巧用Hosts文件 杀掉麻烦的IE浏览器弹出窗口
  17. 创业之路 - 魏杰:下一个 10 年,将造就一批新富翁
  18. mysql导出nb3文件_MySQL导入导出.sql文件
  19. 科研写作——常见句式(五)
  20. location.href跳转url链接失败,原来是零宽字符导致的

热门文章

  1. Dell H310配置no-raid直通模式
  2. 关于注册Google账号时遇到“此电话号码无法用于进行验证”的问题的解决方法
  3. 电脑文件不小心删除了怎么恢复 ? 删除的文件如何恢复文件?
  4. 视觉定位领域专栏(二)常用数据集介绍
  5. mysql外键(FK)及其外键关联操作reference
  6. Android Google AdMob 广告接入配置示例
  7. 【项目经验】最新最全ElasticSearch操作详解
  8. WLS(适用于Windows的Linux子系统)的安装
  9. css overflow属性及使用方法
  10. 推荐 5 款私藏的优质 Chrome 插件