Writing Junit test case for a class with @JobScope, got error as java.lang.IllegalStateException: No Scope registered for scope name 'job'

huangapple go评论81阅读模式
英文:

Writing Junit test case for a class with @JobScope, got error as java.lang.IllegalStateException: No Scope registered for scope name 'job'

问题

在执行测试用例时遇到了以下错误 - 未注册名为'job'的作用域

我的类如下所示:

@Service  
@JobScope  
public class JobService {

    @Value("#{jobParameters['abc']}")
    private ABC abc;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        .......
    }
}

我的测试类如下所示:

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })

@RunWith(SpringRunner.class)  
public class JobServiceTest {

    public JobExecution getJobExecution() {  
        JobExecution execution = MetaDataInstanceFactory.createJobExecution();  
        execution.getExecutionContext().putString("input.data", "foo,bar,spam");  
        return execution;  
    }  

    @Test  
    public void beforeJobTest() {  
        jobService.beforeJob(getJobExecution());
    }
}
英文:

Got this error while executing test cases - No Scope registered for scope name 'job'
> My class looks like:

     @Service  
     @JobScope  
     public class JobService{
    
        @Value("#{jobParameters['abc']}")
        private ABC abc;
    
        @Override
        public void beforeJob(JobExecution jobExecution) {
        .......
        }
      }

>Code My test class looks like:

     @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })  

    @RunWith(SpringRunner.class)  
    public class JobServiceTest{  
    
    public JobExecution getJobExecution() {  
            JobExecution execution = MetaDataInstanceFactory.createJobExecution();  
            execution.getExecutionContext().putString("input.data", "foo,bar,spam");  
            return execution;  
        }  
      
        @Test  
        public void beforeJobTest() {  
            jobService.beforeJob(getJobExecution());
        }
    }

答案1

得分: 1

向测试类添加@EnableBatchProcessing对我有用...但我不确定您是否想要将其放在测试类上。最好使用Config类并在测试类中导入配置。

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })  
@EnableBatchProcessing
@RunWith(SpringRunner.class)  
public class JobServiceTest{  

    public JobExecution getJobExecution() {  
        JobExecution execution = MetaDataInstanceFactory.createJobExecution();  
        execution.getExecutionContext().putString("input.data", "foo,bar,spam");  
        return execution;  
    }  

    @Test  
    public void beforeJobTest() {  
        jobService.beforeJob(getJobExecution());
    }
}
英文:

Adding @EnableBatchProcessing to the test class worked for me... but I'm not sure if you want to put this on a test class. It's probably better to use a Config class and import the configuration in your test class.

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })  
@EnableBatchProcessing
@RunWith(SpringRunner.class)  
public class JobServiceTest{  

    public JobExecution getJobExecution() {  
        JobExecution execution = MetaDataInstanceFactory.createJobExecution();  
        execution.getExecutionContext().putString("input.data", "foo,bar,spam");  
        return execution;  
    }  

    @Test  
    public void beforeJobTest() {  
        jobService.beforeJob(getJobExecution());
    }
}

huangapple
  • 本文由 发表于 2020年1月6日 19:47:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定