SolitudeSky 2018-03-23
解决junit获取注入bean问题:
注解引入多个配置文件情况:
我们使用spring写junit单测的时候,有的时候我们的spring配置文件只有一个。我们在类的注释上面会这样写:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:spring-ctx-application.xml")
但有的时候我们的项目很复杂,其中的spring配置文件被拆分成了多个,这样该如何写上面这段单测代码而引入多个配置文件呢?如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-ctx-application.xml",
"classpath*:spring-ctx-consumer.xml" }) 这样就可以轻松的引入多个spring的配置文件了。
或者配置符合某一个正则表达式的一类文件,如:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:spring-ctx-*.xml")
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCacheLiCai" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!--
timeToIdleSeconds 当缓存闲置n秒后销毁 为了保障会调用ShiroSessionDao的doReadSession方法,所以不配置该属性
timeToLiveSeconds 当缓存存活n秒后销毁 必须比Redis中过期时间短
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToLiveSeconds="3600"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="10"
/>
</ehcache>

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.houbank.basic.util.page.Pagination;
import com.houbank.incoming.api.CacheFacade;
import com.houbank.incoming.api.FinancialSalesUserFacade;
import com.houbank.incoming.model.condition.FinancialSalesUserCondition;
import com.houbank.incoming.web.controller.FinancialSalesUserController;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
/**
* Created by winner_0715 on 2017/1/7.
*/
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations={"classpath*:spring/applicationContext.xml","classpath*:spring/ehcache-shiro.xml"}) //加载配置文件
public class TestGet {
@Autowired
private CacheFacade cacheFacade;
@Autowired
private RestTemplate restTemplate1;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
/**
* 要请求的url,一般放在配置文件中
*/
// @Value(value = "@{remote.url}")
private String remoteUrl;
@Test
public void postRemoteData() throws UnsupportedEncodingException {
/**
* 设置请求头
*/
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
headers.add("Accuept", MediaType.APPLICATION_JSON.toString());
/**
* POST请求参数,根据需要进行封装
*/
String bodyData = "";//new String(Base64Utils.encode(JSON.toJSONString(ro).getBytes("UTF-8")));
/**
* 查看HttpEntity的构造方法,包含只有请求头和只有请求体的情况
*/
HttpEntity<String> httpEntity = new HttpEntity<String>(bodyData, headers);
/**
* 执行操作
*/
FinancialSalesUserCondition cd = new FinancialSalesUserCondition();
remoteUrl="http://localhost:8080/hb_phonebank_web/financialSalesUser/listPage";
// getweixinBaseInfoList();
ResponseEntity<Pagination> result = restTemplate1.postForEntity(remoteUrl,cd,Pagination.class);
}
/**
* 模拟请求参数
*/
private class ParameterRo {
private Integer id = 123;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
public RestTemplate getRestTemplate1() {
return restTemplate1;
}
public void setRestTemplate1(RestTemplate restTemplate1) {
this.restTemplate1 = restTemplate1;
}
public String getRemoteUrl() {
return remoteUrl;
}
public void setRemoteUrl(String remoteUrl) {
this.remoteUrl = remoteUrl;
}
}