autoMock工具开发

开发背景

对于复杂的AO层或者是试用这边的FO层,如果使用easyMock来对其进行单元测试将会给测试人员带来无限的烦恼,因为该层的代码复合了很多的MO或者是外部的一些相关类,这使得easyMock的代码写起来会很麻烦,因为不仅需要mock要测试方法的参数,还要mock内部的一个自动注入属性所调用的方法,这会是一个令测试人员头疼的问题,automock工具就是为了解析这个问题而诞生的。

 

工具运行机制

         测试人员将要测试的类的路径(文件系统的绝对路径)输入并调用工具的生成easymock代法的方法,便能将要测试的类的基础代码全部生成好,并将代码输入至控制台及复制到粘贴板,测试人员直接粘贴代码到测试文件并引入必要的包,设置必要的参数就可以了。

 

工具实现原理

读入java代码,通过ASTParserjava代码进行解析,从中解析出自动注入的属性以及其被调用的方法,使用模板生成工具类进行代码生成

工具实现类图



 
<!--[endif]-->

工具时序图



 
<!--[endif]-->

 

工具运行示例

需要测试的示例代码

public class StudentAO {
 
   private StudentManager studentManager;
   private InfomationManager infomationManager;
 
   public void getSameStudent(StudentDO studentDO,Boolean a){
      int age=studentDO.getAge();
      String name=studentDO.getName();
      if(!isYoungStudent(studentDO,3)){
         return;
      }
     
      studentManager.changeStudentAge(10);
     
      if(infomationManager.isYoungStudent(10)){
         return;
      }
     
      studentManager.isYoungStudent(10);
     
      if(this.isSameStudent()==false){
         return;
      }
     
      infomationManager.changeStudentAge(20);
     
      studentDO=studentManager.getStudentDO("student");
   }
  
   public boolean sameStudent(StudentDO studentDO,Boolean a){
      int age=studentDO.getAge();
      String name=studentDO.getName();
      studentManager.isYoungStudent(10);
     
      if(this.isSameStudent()==false){
         return true;
      }
     
      infomationManager.changeStudentAge(20);
     
      studentDO=studentManager.getStudentDO("student");
      return false;
   }
  
   private boolean isYoungStudent(StudentDO studentDO,int a){
      if(studentDO.getAge()>0){
         return true;
      }
      return false;
   }
  
   private boolean isSameStudent(){
      if(studentManager.isSameStudent(null)){
         return true;
      }
      return isYoungStudent(null,5);
   }
  
  
   public void setStudentManager(StudentManager studentManager) {
      this.studentManager = studentManager;
   }
 
   public void setInfomationManager(InfomationManager infomationManager) {
      this.infomationManager = infomationManager;
   }
 
}

 

 

调用工具生成代码

public static void main(String args[]){
      MockUtil mockUtil=new MockUtil();
      String codeUrl="D:\\study\\automock\\src\\test\\java\\com\\taobao\\matrix\\automock\\util\\StudentAO.java";
      try {
         String code=mockUtil.mockCode(codeUrl);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
 
生成的代码如下
@Test
   public void getSameStudent_test(){
      //实例化需要测试的对象
      StudentAO studentAO=new StudentAO();
 
      //参数及属性相关设置start
      StudentDO studentDO=EasyMock.createMock(StudentDO.class);
      //参数设置start
 
 
 
      //参数设置end
      EasyMock.replay(studentDO);
      //参数及属性相关设置end
 
      //参数及属性相关设置start
      StudentManager studentManager=EasyMock.createMock(StudentManager.class);
      studentManager.changeStudentAge(0);
      EasyMock.expectLastCall();
      EasyMock.expect(studentManager.isYoungStudent(0)).andReturn(false).anyTimes();
      EasyMock.expect(studentManager.getStudentDO("mocktest")).andReturn(null).anyTimes();
      EasyMock.expect(studentManager.isSameStudent((com.taobao.matrix.automock.util.StudentDO)EasyMock.anyObject())).andReturn(false).anyTimes();
      EasyMock.replay(studentManager);
      //参数及属性相关设置end
 
      //参数及属性相关设置start
      InfomationManager infomationManager=EasyMock.createMock(InfomationManager.class);
      EasyMock.expect(infomationManager.isYoungStudent(0)).andReturn(false).anyTimes();
      infomationManager.changeStudentAge(0);
      EasyMock.expectLastCall();
      EasyMock.replay(infomationManager);
      //参数及属性相关设置end
 
      //将属性注入start
      studentAO.setStudentManager(studentManager);
      studentAO.setInfomationManager(infomationManager);
      //将属性注入end
 
      //调用测试方法
      studentAO.getSameStudent(studentDO,false);
      //assert验证区块start
 
 
      //assert验证区块end
   }
 
   @Test
   public void sameStudent_test(){
      //实例化需要测试的对象
      StudentAO studentAO=new StudentAO();
 
      //参数及属性相关设置start
      StudentDO studentDO=EasyMock.createMock(StudentDO.class);
      //参数设置start
 
 
 
      //参数设置end
      EasyMock.replay(studentDO);
      //参数及属性相关设置end
 
      //参数及属性相关设置start
      StudentManager studentManager=EasyMock.createMock(StudentManager.class);
      EasyMock.expect(studentManager.isYoungStudent(0)).andReturn(false).anyTimes();
      EasyMock.expect(studentManager.getStudentDO("mocktest")).andReturn(null).anyTimes();
      EasyMock.expect(studentManager.isSameStudent((com.taobao.matrix.automock.util.StudentDO)EasyMock.anyObject())).andReturn(false).anyTimes();
      EasyMock.replay(studentManager);
      //参数及属性相关设置end
 
      //参数及属性相关设置start
      InfomationManager infomationManager=EasyMock.createMock(InfomationManager.class);
      infomationManager.changeStudentAge(0);
      EasyMock.expectLastCall();
      EasyMock.replay(infomationManager);
      //参数及属性相关设置end
 
      //将属性注入start
      studentAO.setStudentManager(studentManager);
      studentAO.setInfomationManager(infomationManager);
      //将属性注入end
 
      //调用测试方法
      boolean booleanResult=studentAO.sameStudent(studentDO,false);
      //assert验证区块start
 
 
      //assert验证区块end
   }

 

 

猜你喜欢

转载自xiaodongdong.iteye.com/blog/1830006