Рубрики
Без рубрики

Исключение NoSuchBeanDefinitionException : Нет квалифицирующего компонента типа JobLauncherTestUtils

– Исключение NoSuchBeanDefinitionException : Нет квалифицирующего компонента типа JobLauncherTestUtils

Автор оригинала: mkyong.

Следуя официальному руководству по модульному тестированию Spring batch для создания стандартного модульного теста.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/job-abc.xml",
    "classpath:spring/batch/config/context.xml"})

public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        
    }
}

P.S P.S добавляется в путь к классам.

Проблема

При запуске выше модульного теста он запрашивает Вакансии нет такого сообщения об ошибке bean?

org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
	private org.springframework.batch.test.JobLauncherTestUtils com.mkyong.AppTest.jobLauncherTestUtils; 
	......
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
	[org.springframework.batch.test.JobLauncherTestUtils] found for dependency: 
	expected at least 1 bean which qualifies as autowire candidate for this dependency. 
	......
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
	...

Решение

Добавление spring-batch-test.jar в пути к классу не будет автоматически создавать JobLauncherTestUtils компонент.

Чтобы исправить это, объявите компонент JobLauncherTestUtils в одном из ваших конфигурационных файлов Spring.


 
    
    

И загружает его в модульный тест.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/job-abc.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        
    }
}

Рекомендации

  1. Примеры модульных тестов весенней партии – JUnit и TestNG
  2. Официальное руководство по тестированию пружинных блоков

Оригинал: “https://mkyong.com/spring-batch/nosuchbeandefinitionexception-no-qualifying-bean-of-type-joblaunchertestutils/”