转载 Junit手动/自动加载spring配置文件

分配置文件在classpath下和web-inf下两种情况的加载: 

1
2
3
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
new ClassPathXmlApplicationContext("applicationContext.xml");// 从classpath中加载
new FileSystemXmlApplicationContext("classpath:地址");// 没有classpath表示当前目录

  




注解加载 

1
2
3
@org.springframework.test.context.ContextConfiguration(locations={"file:WebRoot/WEB-INF/applicationContext.xml"})
 
@org.springframework.test.context.ContextConfiguration(locations={"classpath:applicationContext.xml"})

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RunWith(SpringJUnit4ClassRunner.class
@ContextConfiguration({"classpath:/ut/applicationContext.xml"})
public class ScanDecisionFileTest {
 
    @Before
    public void setUp() throws Exception {
    }
 
    @After
    public void tearDown() throws Exception {
    }
 
    @Test
    public void testRun() {
        //fail("Not yet implemented");
    }
 
}
posted @ 2018-04-04 17:19  流云165  阅读(217)  评论(0)    收藏  举报