一直忙着敲代码,一直不太注重测试用例的使用,其实使用测试用例是很好的习惯,也能很大程度上提高开发效率。那为什么以前没用呢?因为自己懒,懒得接触新的东西~这种事情还是不能懒的。到最后会发现也是挺简单的。
Mavne+Spring的测试环境搭建:
maven 依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
Test
@ContextConfiguration(locations = { "/spring/spring-*.xml" })
public class Test1 extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private OperationLogDao operationLogDao;
@Test
public void testDao() throws Exception {
List<OperatorLog> list = operationLogDao.selectList(null);
System.out.println("list="+list);
Assert.assertNotNull(list);
}
}
这里需要注意的是要指定配置文件的位置,否则程序会报错。
然后就是推荐使用Assert断言,让程序告诉你是否运行正常