热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot下配置junit测试环境

为什么80%的码农都做不了架构师?项目中使用到了spring_boot,我想在项目中写一些单元测试,但是由于对springboot不

为什么80%的码农都做不了架构师?>>>   hot3.png

项目中使用到了spring_boot,我想在项目中写一些单元测试,但是由于对springboot 不熟悉并且springboot的中文资料非常的少,所以花了很长的时间才把springboot的junit测试环境搭好,虽然很简单,但是也发出来给大家参考一下吧。

一 准备

1 首先编写一个测试环境基类BaseDaoTest

 

package com.gome.superman.web.bussiness;import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/**
* @ClassName: BaseDaoTest
* @Description: TODO
* @author liujie14
* @date 2016年7月8日 下午5:37:27
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DaoConfiguration.class )
@WebIntegrationTest({"server.port=0","management.port=0"})
@ActiveProfiles("test")
public abstract class BaseDaoTest {
}

 

2 编写springboot配置类

 

package com.gome.superman.web.bussiness;
/**
* @Title: DaoConfiguration.java
* @Package com.gome.superman.common.sms
* @Description: TODO
* @author heshengchao
* @date 2016年7月8日 下午5:38:10
* @version V1.0
*/import com.gome.dubbo.DubboAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;/**
* @ClassName: DaoConfiguration
* @Description: TODO
* @author liujie14
* @date 2016年7月8日 下午5:38:10
*
*/
@Configuration
@ComponentScan({"com.gome.superman.web.business"})
@Import({DubboAutoConfiguration.class} )
@ImportResource("classpath:spring/business-dubbo-consumer.xml")
@SpringBootApplication
public class DaoConfiguration {
}


 

 

二 开始编写测试

新建一个TestRest类 继承 BaseDaoTest

 

package com.gome.superman.web.bussiness.controller;import com.gome.superman.common.session.CacheSessionProvider;
import com.gome.superman.util.model.Response;
import com.gome.superman.util.redis.RedisUtils;
import com.gome.superman.web.business.controller.BrandController;
import com.gome.superman.web.business.controller.SuperManTradeRegistController;
import com.gome.superman.web.bussiness.BaseDaoTest;import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;import java.io.Serializable;
import java.util.Map;/*** @Author zengyu* @Date 2016-07-18* @Email zengyu2@gome.com.cn* @Desc*///用于自动化测试整个注册流程
public class testRegist extends BaseDaoTest {@AutowiredSuperManTradeRegistController superManTradeRegistController;@Testpublic void testRgist(){Response response = superManTradeRegistController.validatePhoneNum("18681277109");//效验手机号。Assert.assertTrue( response.isSuccess() );//发送短信 ,获取验证码response = superManTradeRegistController.sendMessage( null , "18681277109" );Assert.assertTrue( response.isSuccess() );String token = response.getResult().toString();String msgCode = getMsgCode( token );response = superManTradeRegistController.messageCodeValidate( token , msgCode );Assert.assertTrue( response.isSuccess() );//根据token获取验证码// 注册资料String msBase = "{ \"tradePhone\": \"18681277109\", \"tradePwd\": \"abcd1111\" }";String msSecurityInfo = "{ \"securityQuestionId\": 1, \"securityQuestionAnswer\": \"sdad\" }";response = superManTradeRegistController.regist( msBase , msSecurityInfo , token );System.out.println( response );Assert.assertTrue( response.isSuccess() );String baseInfos = "{ \"id\": 11, \"companyName\": \"llalal\", \"provinceId\": 1, \"cityId\": 2, \"districtId\": 3, \"provinceName\": \"llalal\", \"cityName\": \"xnclx\", \"districtName\": \"slxx\", \"tradePhone\": 18681277107, \"linkman\": \"sdalnvkd\", \"linkmanPhone\": 18681277109, \"companyPhone\": 2081996, \"companyDetailAddress\": \"nsdadnosfa\" }";String brandsInfo = "[ { \"brandId\": 2, \"brandName\": \"xniovadv\", \"brandEngName\": \"onzxvizo\" } ]";String categoriesInfo = "[]";response = superManTradeRegistController.saveSellerAuthenticationInformation( baseInfos , brandsInfo , categoriesInfo );System.out.println(response);Assert.assertTrue( response.isSuccess() );}public String getMsgCode( String token ){
// Map session = (Map) CacheSessionProvider.unserialize( RedisUtils.binaryGet( token.getBytes()) );
// return (String) session.get("token");return RedisUtils.get(token);}}

 

 

三 注意事项

看了前面一大段代码,相信大家也感觉比较迷糊。使用spingboot一个很重要的地方就是要对它的各种注解都要很熟悉,在这里我就对几个注解说下自己的理解吧。

先看两个注解:

@Import   @ImportResource

他们在springboot的官方文档中是这样介绍的

/**
* Indicates one or more {@link Configuration @Configuration} classes to import.
*
*

Provides functionality equivalent to the {@code } element in Spring XML.
* Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
* {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
* classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
*
*

{@code @Bean} definitions declared in imported {@code @Configuration} classes should be
* accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
* injection. Either the bean itself can be autowired, or the configuration class instance
* declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly
* navigation between {@code @Configuration} class methods.
*
*

May be declared at the class level or as a meta-annotation.
*
*

If XML or other non-{@code @Configuration} bean definition resources need to be
* imported, use the {@link ImportResource @ImportResource} annotation instead.

意思就是。 @import 相当于我们在进行xml配置中的标签。用来包含一些被@Configruation标记的类。

@ImportResource  用来引入一些xml文件,从而使我们可以既用@Configuration类来配置,又可以用xml文件进行配置。

再看@SpringBootApplication

这个注解在源码中是这样定义的

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

可见,使用了这个注解后,被标记的类就拥有@ComponentScan  @Configuration 等等的特性。


转:https://my.oschina.net/boltwu/blog/716604



推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
author-avatar
U友50089076
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有