<!--[if !supportLists]-->3. <!--[endif]-->使用过程,大体分为3步,如下:
<!--[if !supportLists]-->a) <!--[endif]-->配置: 配置切面,选择mock触发点,配置连接点的逻辑,选择处理方式
<bean id="mockAspect" class="com.taobao.azeroth.util.MockTestAspect" > <property name="sleepTime" value="0"/> <property name="flag" value="false"/> </bean> <aop:config proxy-target-class="false" > <aop:pointcut id="springTestPointcut" expression="execution(* com..Hello.hello(..))" /> <aop:aspect id="aspectSpringSleep" ref="mockAspect" order="100"> <aop:around method="mockMethod" pointcut-ref="springTestPointcut" /> </aop:aspect> </aop:config> |
<!--[if !supportLists]-->b) <!--[endif]-->脚本: 配置切面参数&调用被测试方法
<!--[if !supportLists]-->c) <!--[endif]-->执行策略: 是否要多线程&构造单个线程的参数&配置执行策略,运行
@Test public void test_01_thread_run(){ ThreadFactory thFac=new ThreadFactory(); //配置切面信息 mockAspect.setMockResult(11); ThreadTest th=new ThreadTest(); th.setOwnerBean(hello); th.setMethodName("hello"); th.setMockAspect(mockAspect); th.setThreadMockFlag(true); //一个线程可以有n个校验服务 AssertService as=new AssertService(); //增加线程中的验证方法,直接可以定义 as.setCheckMethod(this, "checkResult"); th.addAssertService(as); //通过传单个mock thread来传参 thFac.addThread(th); //同时启动5个线程来测试 thFac.startThreads(5); } public void checkResult(Integer result){ System.out.println("传入设置actual值得方法"); result=result+1; assertThat("验证",result,greaterThan(10)); } |
总结:使用AOP的方式来注入mock内容,可以使我们的测试代码更清晰,利于维护管理,也可以更好的扩展我们的mock时要进行的操作.
相关文章: