最近在调研JUnit单元测试。因为分层的关系,涉及到很多的Spring注入,在底层业务类测试的时候发现需要读取Spring的一些配置文件。

以下是读取配置文件的例子:

 

 
  
  1. import static org.junit.Assert.assertEquals; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5.  
  6. import org.hibernate.SessionFactory; 
  7. import org.hibernate.classic.Session; 
  8. import org.junit.Before; 
  9. import org.junit.Test; 
  10. import org.springframework.context.ApplicationContext; 
  11. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  12.  
  13.  
  14. public class TestService { 
  15.  
  16.     private ApplicationContext ac; 
  17.      
  18.     @Before 
  19.     public void setUp() throws Exception{ 
  20.         ac = new ClassPathXmlApplicationContext("config/*.xml"); 
  21.     } 
  22.      
  23.     @Test 
  24.     public void TestApplicationInfoSucess() throws Exception{ 
  25.         //具体的业务 
  26.     } 
  27.