首页 > 技术文章 > 使用junit4测试Spring

dorothyorsusie 2016-12-14 14:42 原文

Spring 提供便捷的测试,非常方便整合Junit

导入 spring-test-3.2.0.RELEASE.jar  ---- 提供与Junit的整合 
@RunWith(SpringJUnit4ClassRunner.class) // 整合 
@ContextConfiguration(locations="classpath:applicationContext.xml") // 加载配置
public class HelloServiceTest {
@Autowired // 注入
private HelloService helloService;

@Test 
public void testSayHello() {
helloService.sayHello();
}
}
通过配置 加载Spring配置文件,将需要测试对象 注入到 测试类中,在测试方法中 直接使用对象即可

推荐阅读