以前开发web项目从来不喜欢用单元测试,每次都需要启动服务器,在浏览器中调试,有些错误还不一定发现得到。
最近公司开发一个项目,任务繁重,不由觉得以前那种测试模式太笨拙了,于是学习了使用Junit;
不多说,直接上代码:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext.xml"})
public class Test {
@Autowired
HttpServletRequest request;
//用@Autowired将需要测试的类注入
@org.junit.Test
public void testGetCount() {
//这里直接用注入的类调用需要测试的方法就行了。
}
}
当然前提还是需要一些必要的jar包。