作为程序员,我们自己在写完程序的时候应该先做完单元测试,确认无误以后才把代码提交到公司公共的系统里面去,这样做其实也有利于提高自己的能力,这里做了个简单的单元测试案列写法。
上代码:
/**
* 配置Spring与junit4整合,junit启动时加载spring IOC容器
* spring-test ,junit
*/
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit,spring的配置文件
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class TestTableA {
//注入dao类实现类依赖
@Resource
private TableADAO taDAO;
@Test
public void testQueryById(){
Integer id = 20;
TableA tableA = taDAO.queryById(id);
System.out.println(tableA.getName());
}
到这里一个简单的根据id查询数据的单元测试就做完了。