StrongZhu的博客
StrongZhu · 共 8 篇文章
提取mybatis mapper接口
在使用mybatis 3.0的annotation之后,会产生大量mapper接口文件。 使用下面这个类可以提取这些mapper的清单 public final class MybatisMapperScanner extends ClassPathScanningCandidateComponentProvider { { addIncludeFilter(new AnnotationTypeFilter(Repository.class)); // exclude package-info.java addExcludeFilter(new TypeFilter() { @Overrid…
获取类的所有子类
开发过程中,有时需要获取某个包下的所有类,或者基于指定规则获取类清单。 比较常见的解决方案是自己遍历目录,查找所有.class文件。 下面这个方法使用spring工具类实现,简化过程,不再需要自己遍历目录 /** * 获取在指定包下某个class的所有非抽象子类 * * @param parentClass * 父类 * @param packagePath * 指定包,格式如"com/iteye/strongzhu" * @return 该父类对应的所有子类列表 */ @SneakyThrows(ClassNotFoundException.class) public static <E>…
2013-03-16 19:15 · spring · 评论(0)
更有效的mock,优化spring框架下的mock方法
本文主要是提供了一种解决方案,用于解决spring管理的测试用例在mock过程中,如何有效管理mock宿主和mock实体,并优化mock方法 一、基础类 1、Sping配置基础 @ContextConfiguration(locations = { "classpath:spring.xml" }) public abstract class BaseServiceTest extends AbstractTransactionalJUnit4SpringContextTests { /** * 日志器 */ protected Logger logger = LoggerFactory.g…
2011-08-06 11:59 · TDD · 评论(1)
使用jmock测试System.in和System.out
本文要求读者已具备juit和jmock基础 在TDD过程中需要测试代码中的System.in和System.out。 技术难点包括 1、mock宿主还原 2、inout参数的行为模拟 3、mock system.in和out 1、测试主体 @Service class ConsoleManagerBizImpl{ private static Logger LOGGER = Logger .getLogger(ConsoleManagerBizImpl.class); public String readLine() { final Scanner scanner = new Scanner…
2011-08-06 11:39 · TDD · 评论(0)
使用mybatis3.0 annotation接口简化数据访问
数据访问进化简史,比较简单,不负责能跑起来 一、原始的sql select address from user where name='test' 二、jdbc //若干操作 String sql="select address from user where name='"+name+"'"; //执行 //解析结果 //包括若干个try catch 解决了java到数据库的访问,但是存在大量的复制代码,并且异常需要自己处理 三、ibatis2.0代码 <!--大量的XML定义文件--> <select id="queryAddressByName" parameterType="Stri…
2011-07-31 00:11 · 评论(0)
使用工厂方法mock测试方法体内中new出来的对象
本文假设读者已了解mock的基本定义,基础使用场景。本文的mock框架采用jmock 1、mock经典测试场景 class ToTest { private Member m; public void test(Varible v){ m.call(); v.call(); } } 通过mock出一个m和v,并对其行为进行预测,就可以对ToTest.test()进行单元测试 2、本文关注的问题为以下使用场景 Class ToTestA{ public int test(){ SomeClass s = new SomeClass(); s.call(); } } 这样的情况下,如何才能对To…
2011-07-22 10:29 · 评论(0)
另一个hosts文件
66.249.89.99 docs.google.com 66.249.89.99 spreadsheets.google.com 66.249.89.99 spreadsheets0.google.com 66.249.89.99 docs1.google.com 66.249.89.99 clients1.google.com 66.249.89.99 clients2.google.com 66.249.89.99 webcache.googleusercontent.com 66.249.89.99 sites.google.com 66.249.89.99 talkgadget.g…
2011-07-20 13:23 · 评论(0)
不使用中间变量交换两个int值
int a=1; int b=2; a=a+b;//a=3,b=2 b=a-b;//a=3,b=1 a=a-b;//a=2,b=1
2011-07-19 10:47 · 评论(0)