单元测试,我应该修改方法参数吗?



我目前正在做一个令人讨厌的遗留项目,我很乐意将单元测试添加到我修复的任何东西中。

public abstract class AbstractYYY extends XX {
   private LoginDTO selectedLogin;
   private Tab tab;
   private GenericJobDTO job;

   @Override
   public void action() throws TabException {
     JobContainerDTO jobContainer = tab.getRoot().getData();
     jobContainer.setUser(tab.getUsername());
     jobContainer.setServer(tab.getConnectorURL());
     jobContainer.setPlatform(tab.getPlatform());
     jobContainer.setLoginID(selectedLogin.getId());
     if (jobContainer.getApp().equals(App.TEST)) {
        if (job instanceof JobDTO) {
            ((JobDTO) job).setLoginId(selectedLogin.getId());
            ((JobDTO) job).setZoneId(jobContainer.getZoneId());
        }
        if (job instanceof JobAutoDTO) {
            ((JobAutoDTO) job).setLogin(selectedLogin.getId());
            if(selectedLogin.getConnector() != null ){
                ((JobAutoDTO) job).setConnectorId(selectedLogin.getConnector().getId());
            }
        }
     }

     if (jobContainer.getStatus() != 1 ) {
        jobContainer.setLastRunStartTime(0);
        jobContainer.setLastRunEndTime(0);
        jobContainer.setLog("");
     }
     job.setId(UUID.randomUUID().toString());
     jobContainer.setJobXYZasJSON(job.toJson());
     jobContainer.setWorker(job.getWorker());
     jobContainer.setId(savedID);

     if (jobController != null) {
        jobController.action();
     }
 }
.
.
.
.
}

我想测试我的jobContainer是如何填充的,所以我创建了一个类似

JobContainerDTO

setContainerInfo(JobContainerDTO jobContainer, Tab tab...)

但这意味着我将不得不修改参数jobContainer,这似乎不是一个好主意。

您将如何测试此方法?

我会创建两个辅助类。

一个用于填充作业

容器,另一个专门用于填充作业。

作业容器解析器

public class JobContainerResolver{
   private JobContainer jobContainer;
   public JobContainerResolver(JobContainer jobContainer){
        this.jobContainer = jobContainer;
   }
   public void fillWithTabData(Tab tab){
        jobContainer.setUser(tab.getUsername());
        jobContainer.setServer(tab.getConnectorURL());
        jobContainer.setPlatform(tab.getPlatform());
   }
   public void fillWithJobData(Job job){
       jobContainer.setJobXYZasJSON(job.toJson());
       jobContainer.setWorker(job.getWorker());
   }
   public void fillWithIds(SelectedLogin login, Integer savedID){
        jobContainer.setId(savedID);
        jobContainer.setLoginID(selectedLogin.getId());
   }
}

作业解析器

public class JobResolver{
    private Job job;
    public JobResolver(Job job){
         this.job = job;
    }   
    public fillWithBasicData(JobContainer, SelectedLogin){
        if (jobContainer.getApp().equals(App.TEST)) {
           if (job instanceof JobDTO) {
             ((JobDTO) job).setLoginId(selectedLogin.getId());
             ((JobDTO) job).setZoneId(jobContainer.getZoneId());
           }
           if (job instanceof JobAutoDTO) {
             ((JobAutoDTO) job).setLogin(selectedLogin.getId());
             if(selectedLogin.getConnector() != null ){
                   ((JobAutoDTO) job).setConnectorId(selectedLogin.getConnector().getId());
             }
           }
        }
    }
}

操作() 方法

@Override
public void action() throws TabException {
    JobContainerDTO jobContainer = tab.getRoot().getData();
    JobContainerResolver jcResolver = buildJobContainerResolver(jobContainer);
    JobResolver jResolver = buildJobResolver(job);
    jcResolver.fillWithTabData(tab); 
    jcResolver.fillWithJobData(job);
    jcResolver.fillWithIds(selectedLogin, savedID);
    jResolver.fillWithBasicData(jobContainer, selectedLogin);
}
public JobContainerResolver buildJobContainerResolver(JobContainer jc){
    return new JobContainerResolver(jc);
}
public JobResolver buildJobResolver(Job job){
    return new JobResolver(job);
}

使用此结构,您可以单独测试不同的设置和 action() 方法本身。你只需要玩一下嘲笑。

您可以模拟Tab tab字段,以便它返回您可以断言的对象。

@Mock
private Tab tab;
@InjectMocks
private AbstractYYY testClass = new RealYYY;
@Test
public void test(){
    JobContainerDTO jobContainer = new JobContainerDTO();
    Root mockedRoot = mock(Root.class);
    when(tab.getRoot).thenReturn(mockedRoot);
    when(mockedRoot.getData()).thenReturn(jobContainer);
    testClass.action();
   //Here you can assert that JobContainerDTO jobContainer is in the state that you expect it to be.
}

同样的方法也可以用GenericJobDTO job来完成。您需要模拟它,然后验证方法调用。

相关内容

  • 没有找到相关文章

最新更新