springboot下配置junit测试环境

项目中使用到了spring_boot,我想在项目中写一些单元测试,可是因为对springboot 不熟悉而且springboot的中文资料很是的少,因此花了很长的时间才把springboot的junit测试环境搭好,虽然很简单,可是也发出来给你们参考一下吧。java

一 准备

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类 继承 BaseDaoTestweb

 

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 {
    @Autowired
    SuperManTradeRegistController superManTradeRegistController;


    @Test
    public 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<String, Serializable> session = (Map<String, Serializable>) CacheSessionProvider.unserialize( RedisUtils.binaryGet(  token.getBytes()) );
//        return (String) session.get("token");
        return RedisUtils.get(token);
    }



}

 

 

三 注意事项

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

先看两个注解:spring

@Import   @ImportResourcespringboot

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

/**
 * Indicates one or more {@link Configuration @Configuration} classes to import.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} 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}).
 *
 * <p>{@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.
 *
 * <p>May be declared at the class level or as a meta-annotation.
 *
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use the {@link ImportResource @ImportResource} annotation instead.

意思就是。 @import 至关于咱们在进行xml配置中的<import/>标签。用来包含一些被@Configruation标记的类。app

@ImportResource  用来引入一些xml文件,从而使咱们能够既用@Configuration类来配置,又能够用xml文件进行配置。ide

再看@SpringBootApplication单元测试

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

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

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

相关文章
相关标签/搜索