一般情况下:
@ContextConfiguration(Locations="../applicationContext.xml")
多个文件时,可用{}
@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version> 3.2.4.RELEASE </version>
<scope>provided</scope>
</dependency>
一个小 demo
import com.web.service.impl.UpmServiceImpl;
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;
/**
* Author: HeatDeath
* Date: 2018/2/6
* Desc:
*/
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //加载配置文件
public class UpmServiceTest {
@Autowired
private UpmServiceImpl upmService;
@Test
public void testUpmService() {
System.out.println(upmService.getAppKey());
System.out.println(upmService.getJetAppId());
System.out.println(upmService.getUpmUrl());
System.out.println(upmService.validAccess("123", "123"));
}
}
1、Spring整合JUnit4测试时,使用注解引入多个配置文件
http://blog.csdn.net/lutinghuan/article/details/18597841
2、Java Spring 【@ContextConfiguration】java世界的那些注解
http://blog.csdn.net/fuyifang/article/details/52792012
3、聊聊单元测试(三)——Spring Test+JUnit完美组合
http://blog.csdn.net/shan9liang/article/details/40452469