reset password
Author Message
hgadhia
Posts: 52
Posted 15:15 Jan 27, 2015 |

Hi,

I have created a function to test if a user with username jdoe1 exists. After running the UserDaoTest for Test NG, I am getting the following error in console. 

FAILED CONFIGURATION: @BeforeClass springTestContextPrepareTestInstance
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gefp.model.dao.UserDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: gefp.web.model.dao.UserDao gefp.model.dao.UserDaoTest.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [gefp.web.model.dao.UserDao] 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:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
    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:307)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: gefp.web.model.dao.UserDao gefp.model.dao.UserDaoTest.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [gefp.web.model.dao.UserDao] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 30 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [gefp.web.model.dao.UserDao] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 32 more

 

Here's the code snippets that I am using for test purpose.

 

UserDaoImpl.java

    @Override
    public Users getUserByName(String username) {
        
        return entityManager.createQuery( "from Users where username = " + username, Users.class ).getSingleResult();
        
    }

 

UserDaoTest.java

    @Test
    public void getUserByName()
    {
        assert userDao.getUserByName("jdoe1").getUsername().equalsIgnoreCase( "jdoe1" );
    }

 

Can someone please help?

 

sikandarahmad
Posts: 19
Posted 15:20 Jan 27, 2015 |

Did you define the bean in Test class and annotate it wit autowire. 

for example:

@Autowired
    UserDao userDao;

akshays
Posts: 5
Posted 15:22 Jan 27, 2015 |

Your UserDaoTest needs to extend  "AbstractTransactionalTestNGSpringContextTests", can you make sure you have done that?

hgadhia
Posts: 52
Posted 15:22 Jan 27, 2015 |

Yes I have done that.

Basically I just added a new function to get the User with a user name jdoe1. And all the required functions in respective interfaces and classes.

Rest everything is same as the the example code.

cysun
Posts: 2935
Posted 15:36 Jan 27, 2015 |

You probably missed the @repository annotation in UserDaoImpl.

hgadhia
Posts: 52
Posted 15:41 Jan 27, 2015 |
cysun wrote:

You probably missed the @repository annotation in UserDaoImpl.

I have checked, its there. I didn't modify anything in the sample code from springmvc, else except for creating the functions as mentioned above. And I also added this function to the UserDao interface.

hgadhia
Posts: 52
Posted 15:44 Jan 27, 2015 |
hgadhia wrote:
cysun wrote:

You probably missed the @repository annotation in UserDaoImpl.

I have checked, its there. I didn't modify anything in the sample code from springmvc, else except for creating the functions as mentioned above. And I also added this function to the UserDao interface.

I also replaced springmvc with gefp in the package name.

I also updated the base package in applicationContext.xml

<context:component-scan base-package="gefp.model" />

I am not sure what am I missing.

cysun
Posts: 2935
Posted 15:45 Jan 27, 2015 |
hgadhia wrote:
cysun wrote:

You probably missed the @repository annotation in UserDaoImpl.

I have checked, its there. I didn't modify anything in the sample code from springmvc, else except for creating the functions as mentioned above. And I also added this function to the UserDao interface.

The error says : No qualifying bean of type [gefp.web.model.dao.UserDao] found for dependency, which means it couldn't find a bean that implements the UserDao interface. There are really just a few possibilities:

  • The @repository annotation was missing so Spring didn't consider UserDaoImpl a bean.
  • UserDaoImpl didn't implement UserDao.
  • The package in <component-scan> was wrong so Spring didn't scan the package that contained UserDaoImpl.
cysun
Posts: 2935
Posted 15:47 Jan 27, 2015 |
hgadhia wrote:
hgadhia wrote:
cysun wrote:

You probably missed the @repository annotation in UserDaoImpl.

I have checked, its there. I didn't modify anything in the sample code from springmvc, else except for creating the functions as mentioned above. And I also added this function to the UserDao interface.

I also replaced springmvc with gefp in the package name.

I also updated the base package in applicationContext.xml

<context:component-scan base-package="gefp.model" />

I am not sure what am I missing.

Reading the error message, it seems like your model classes are under gefp.web.model, i.e. you don't have a gefp.model package.

hgadhia
Posts: 52
Posted 15:57 Jan 27, 2015 |
cysun wrote:
hgadhia wrote:
hgadhia wrote:
cysun wrote:

You probably missed the @repository annotation in UserDaoImpl.

I have checked, its there. I didn't modify anything in the sample code from springmvc, else except for creating the functions as mentioned above. And I also added this function to the UserDao interface.

I also replaced springmvc with gefp in the package name.

I also updated the base package in applicationContext.xml

<context:component-scan base-package="gefp.model" />

I am not sure what am I missing.

Reading the error message, it seems like your model classes are under gefp.web.model, i.e. you don't have a gefp.model package.

 

 

All my model classes in the are under gefp.web.model package under src/main/java folder.

 Its same as explained in the example code.

I am attaching the screenshots of the classes if that helps to find the issue.

 

Attachments: