1. 程式人生 > >Sprin g整合 JUnit4 測試時,使用 @ContextConfiguration 註解引入多個配置檔案

Sprin g整合 JUnit4 測試時,使用 @ContextConfiguration 註解引入多個配置檔案

一般情況下:

@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")); } }