英文:
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());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论