Spring/Spring的单元测试

使用步骤

  1. 导包: Spring单元测试包spring-test-4.0.0.RELEASE.jar
  2. @ContextConfiguration(locations=””)来指定Spring的配置文件
  3. @RunWith指定用哪种驱动进行单元测试,默认JUnit

    @RunWith(SpringJUnit4ClassRunner.class)
    使用Spring的单元测试模块来执行标了@Test的测试方法
    好处是不需要获取Bean

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @ContextConfiguration(locations="classpath:ioc.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    public class SpringIOCTest {
    ApplicationContext ioc = null;

    @Autowired
    BookServlet bookServlet;

    @Test
    public void test() {
    System.out.println("--"+bookServlet);
    }
    }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×