如何编写Junit测试代码
2017-02-16 17:19
337 查看
如何编写Junit测试代码
一、环境准备:
jdk1.7+,Junit4.9,Spring3.2.0。
二、测试代码部分:
DataSynchReceiveServiceImpl.java
DataSynchReceiveServiceImplTest.java
三、测试心得:
对于运行测试代码,不需要启动任何服务,但是特别需要注意的是,一定要有正确的配置文件路径,否则测试程序很容易运行失败。
学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!
一、环境准备:
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); @Autowired private CustomerInfoService customerInfoService; @Autowired private SubscriberInfoService subscriberInfoService; @Autowired private SubscriberSubscriptionService sss; @Autowired private AccountTrafficService accountTrafficService; @Autowired private AccountDurationService accountDurationService; @Autowired private SubscriptionInfoService subscriptionInfoService; @Autowired private ProductInfoService productInfoService; @Autowired private ProductFeeRuleService productFeeRuleService; @Autowired private ProductParameterService productParameterService; @Autowired private ProductServiceService productServiceService; @Autowired private ServiceInfoService serviceInfoService; @Autowired private AccountInfoService accountInfoService; @Autowired private 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{ @Autowired private ProductInfoService productInfoService; @Autowired private ProductServiceService productServiceService; @Autowired private ServiceInfoService serviceInfoService; @Autowired private ProductParameterService productParameterService; @Autowired private ProductFeeRuleService productFeeRuleService; /** * 测试产品信息的保存 * @param * @return */ @Test public 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 */ @Test public 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 */ @Test public 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 */ @Test public 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 */ @Test public 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; @Before public void preTs() throws Exception { context = new ClassPathXmlApplicationContext("classpath:spring/springmvc.xml"); } }
三、测试心得:
对于运行测试代码,不需要启动任何服务,但是特别需要注意的是,一定要有正确的配置文件路径,否则测试程序很容易运行失败。
学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!
相关文章推荐
- 如何使用Junit测试使用Spring框架的代码
- 06-编写Hibernate API编写访问数据库的代码,使用Junit进行测试
- 如何使用Junit编写和组织测试程序
- Junit测试代码编写命名规范
- Junit测试代码编写命名规范
- MyEclipse如何编写JUnit测试类
- 如何使用Junit编写和组织测试程序
- junit测试spring整合ActiveMQ编写消费者代码报错:org.springframework.beans.factory.BeanCreationException: Error crea
- 如何编写测试代码
- 在VS2010单元测试中,Assert断言主要有那些功能?如何去编写代码测试?
- 如何使用Junit编写和组织测试程序
- JUnit 编写测试用例的建议
- 如何编写高质量的代码——来自《代码大全(第2版)》的启示
- [精华]如何编写高质量的VB代码
- 如何编写异常安全的C++代码
- 如何编写高质量的VB6代码
- 编写JUnit测试的理由和几条测试习惯
- 如何编写异常安全的C++代码
- 如何成功把EXCEL表的数据导入到SQL数据库,代码如何编写
- 如何为嵌入式应用编写优秀的C++代码