Main Tutorials

NoSuchBeanDefinitionException : No qualifying bean of type JobLauncherTestUtils

Following the official Spring batch unit testing guide to create a standard unit test case.


@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 spring-batch-test.jar is added to the classpath.

Problem

When launches above unit test, it prompts JobLauncherTestUtils no such bean error message?


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)
	...

Solution

Adding spring-batch-test.jar into the classpath will not create the JobLauncherTestUtils bean automatically.

To fix it, declares a JobLauncherTestUtils bean in one of your Spring configuration file.

spring/batch/config/test-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 
    <bean class="org.springframework.batch.test.JobLauncherTestUtils"/>
    
</beans>

And loads it into the unit test.


@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());
        
    }
}

References

  1. Spring batch unit test examples – jUnit and TestNG
  2. Spring batch unit testing official guide

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
reddynr
7 years ago

Hi All,

I am getting error : No qualifying bean of type . Please advice is highly appreciated. Thanks

[com.bns.mcac.config.DatabaseConfig] found for dependency [com.bns.mcac.config.DatabaseConfig]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Anitha
8 years ago

hi Mkyong.. First of all thank you for posts as many posts saved mytime and cleared my doubts.
I have a question in Spring Junit (am a beginner).. while defining the bean class it says noclassdef found. This is because the test-configuration.xml is inside my test folder and all the bean classes are outside test folder. How do i resolve this?

reddynr
7 years ago

Hi mkyong,

I am getting below error. Could you please advice if anything I messed. Thanks

Error:expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)

Barna Adrian
8 years ago

If you would added how to set JobLauncherTestUtils also from a configuration class:

@Bean

public JobLauncherTestUtils jobLauncherTestUtils() {

return new JobLauncherTestUtils();

}

This article would be perfect. Anyway, like always your articles are state of the art. A really thank you for them 😉

sudhindra
10 years ago

hi … when i run this file, i am getting error i.e. nosuchbeandefinitionexception

package service;

import java.util.List;
import model.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringHibernateDaoSupport {

public static void main(String[] args) {
System.out.println(“************** BEGINNING PROGRAM **************”);

ApplicationContext context = new ClassPathXmlApplicationContext(“**/dispatcher-servlet.xml”);
PersonService personService = (PersonService) context.getBean(“personService”);

Person person = new Person();
person.setName(“Alba”);
person.setEmail(“[email protected]”);
person.setId(“alba”);
personService.addPerson(person);
System.out.println(“Person : ” + person + ” added successfully”);

List persons = personService.fetchAllPersons();
System.out.println(“The list of all persons = ” + persons);

System.out.println(“************** ENDING PROGRAM *****************”);
}
}

error:
run:
************** BEGINNING PROGRAM **************
Aug 24, 2013 6:30:34 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2d9ffd6f: startup date [Sat Aug 24 18:30:34 IST 2013]; root of context hierarchy
Aug 24, 2013 6:30:34 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@606dbdc1: defining beans []; root of factory hierarchy
Exception in thread “main” org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘personService’ is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at service.TestSpringHibernateDaoSupport.main(TestSpringHibernateDaoSupport.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
can you please let me know how to correct it…
thanks in advance

sudhindra
10 years ago

hi … when i run this file, i am getting error i.e. nosuchbeandefinition

package service;

import java.util.List;
import model.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringHibernateDaoSupport {

public static void main(String[] args) {
System.out.println(“************** BEGINNING PROGRAM **************”);

ApplicationContext context = new ClassPathXmlApplicationContext(“**/dispatcher-servlet.xml”);
PersonService personService = (PersonService) context.getBean(“personService”);

Person person = new Person();
person.setName(“Alba”);
person.setEmail(“[email protected]”);
person.setId(“alba”);
personService.addPerson(person);
System.out.println(“Person : ” + person + ” added successfully”);

List persons = personService.fetchAllPersons();
System.out.println(“The list of all persons = ” + persons);

System.out.println(“************** ENDING PROGRAM *****************”);
}
}

error:
run:
************** BEGINNING PROGRAM **************
Aug 24, 2013 6:30:34 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2d9ffd6f: startup date [Sat Aug 24 18:30:34 IST 2013]; root of context hierarchy
Aug 24, 2013 6:30:34 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@606dbdc1: defining beans []; root of factory hierarchy
Exception in thread “main” org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘personService’ is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at service.TestSpringHibernateDaoSupport.main(TestSpringHibernateDaoSupport.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
can you please let me know how to correct it…
thanks in advance