最近在调研JUnit单元测试。因为分层的关系,涉及到很多的Spring注入,在底层业务类测试的时候发现需要读取Spring的一些配置文件。
以下是读取配置文件的例子:
- import static org.junit.Assert.assertEquals;
- import java.util.ArrayList;
- import java.util.List;
- import org.hibernate.SessionFactory;
- import org.hibernate.classic.Session;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestService {
- private ApplicationContext ac;
- @Before
- public void setUp() throws Exception{
- ac = new ClassPathXmlApplicationContext("config/*.xml");
- }
- @Test
- public void TestApplicationInfoSucess() throws Exception{
- //具体的业务
- }
- }
转载于:https://blog.51cto.com/yujie020/978271