我目前正在处理一个讨厌的遗留项目,我很乐意将单元测试添加到我修复的任何内容中。
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
,这似乎不是一个好主意。
您如何测试此方法?
答案 0 :(得分:2)
我会创建两个辅助类。
一个用于填充jobContainer,另一个用于填充作业。
<强> JobContainerResolver 强>
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());
}
}
<强> JobResolver 强>
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());
}
}
}
}
}
action()方法
@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()方法本身。你只需要嘲笑一下。
答案 1 :(得分:1)
您可以模拟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
可以采用相同的方法。你需要模拟它,然后验证方法调用。