项目中使用到了spring_boot,我想在项目中写一些单元测试,但是由于对springboot 不熟悉并且springboot的中文资料非常的少,所以花了很长的时间才把springboot的junit测试环境搭好,虽然很简单,但是也发出来给大家参考一下吧。
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 {
}
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 [email protected]
* @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 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意思就是。 @import 相当于我们在进行xml配置中的} 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.
@ImportResource 用来引入一些xml文件,从而使我们可以既用@Configuration类来配置,又可以用xml文件进行配置。
再看@SpringBootApplication
这个注解在源码中是这样定义的
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication {
可见,使用了这个注解后,被标记的类就拥有@ComponentScan @Configuration 等等的特性。