<1>讀取檔案:

配置檔案在classes下:locations = {"classpath*:/spring/applicationContext.xml"}

配置檔案在web-inf下:locations = {"file:web/WEB-INF/applicationContext.xml"}

<2>例項類:

package com.test;

import java.util.List;

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;

import com.entity.User;
import com.service.impl.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:web/WEB-INF/applicationContext.xml"})
public class PageTest {

@Autowired
private UserService userService;

@Test
public void testPage(){
  //  add your  test  code
}
}

}