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

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

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

问题

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

我的类如下所示:

  1. @Service
  2. @JobScope
  3. public class JobService {
  4. @Value("#{jobParameters['abc']}")
  5. private ABC abc;
  6. @Override
  7. public void beforeJob(JobExecution jobExecution) {
  8. .......
  9. }
  10. }

我的测试类如下所示:

  1. @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })
  2. @RunWith(SpringRunner.class)
  3. public class JobServiceTest {
  4. public JobExecution getJobExecution() {
  5. JobExecution execution = MetaDataInstanceFactory.createJobExecution();
  6. execution.getExecutionContext().putString("input.data", "foo,bar,spam");
  7. return execution;
  8. }
  9. @Test
  10. public void beforeJobTest() {
  11. jobService.beforeJob(getJobExecution());
  12. }
  13. }
英文:

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

  1. @Service
  2. @JobScope
  3. public class JobService{
  4. @Value("#{jobParameters['abc']}")
  5. private ABC abc;
  6. @Override
  7. public void beforeJob(JobExecution jobExecution) {
  8. .......
  9. }
  10. }

>Code My test class looks like:

  1. @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })
  2. @RunWith(SpringRunner.class)
  3. public class JobServiceTest{
  4. public JobExecution getJobExecution() {
  5. JobExecution execution = MetaDataInstanceFactory.createJobExecution();
  6. execution.getExecutionContext().putString("input.data", "foo,bar,spam");
  7. return execution;
  8. }
  9. @Test
  10. public void beforeJobTest() {
  11. jobService.beforeJob(getJobExecution());
  12. }
  13. }

答案1

得分: 1

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

  1. @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })
  2. @EnableBatchProcessing
  3. @RunWith(SpringRunner.class)
  4. public class JobServiceTest{
  5. public JobExecution getJobExecution() {
  6. JobExecution execution = MetaDataInstanceFactory.createJobExecution();
  7. execution.getExecutionContext().putString("input.data", "foo,bar,spam");
  8. return execution;
  9. }
  10. @Test
  11. public void beforeJobTest() {
  12. jobService.beforeJob(getJobExecution());
  13. }
  14. }
英文:

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.

  1. @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, JobScopeTestExecutionListener.class })
  2. @EnableBatchProcessing
  3. @RunWith(SpringRunner.class)
  4. public class JobServiceTest{
  5. public JobExecution getJobExecution() {
  6. JobExecution execution = MetaDataInstanceFactory.createJobExecution();
  7. execution.getExecutionContext().putString("input.data", "foo,bar,spam");
  8. return execution;
  9. }
  10. @Test
  11. public void beforeJobTest() {
  12. jobService.beforeJob(getJobExecution());
  13. }
  14. }

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:

确定