使用Spring的单元测试:
1.导包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
2.加上两个注解:
@ContextConfiguration(locations = “classpath:spring1.xml”)用来指定配置文件的位置
@RunWith(SpringJUnit4ClassRunner.class)使用Spring的单元测试模块来执行标注了@Test注解的测试方法
好处:不用通过getBean()方法获取实例,而是直接通过Autowired注解
@ContextConfiguration(locations = "classpath:spring1.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest1 {
ApplicationContext context = null;
@Autowired
BookService bookService;
@Autowired
BookController bookController;
@Test
public void test(){
// BookController bookController =
// context.getBean("book", BookController.class);
System.out.println(bookController);
}
}