Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9285575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:12:03+00:00 2026-06-18T19:12:03+00:00

I’m getting errors trying to inject resource dependencies into my unit testing. My approach

  • 0

I’m getting errors trying to inject resource dependencies into my unit testing.
My approach has been to write a TestConfig.java to replace the applicationContext.xml for production which manages the connections of the beans. So that I can run it with an in-memory database and just test components.

TestConfig.java

@Configuration
@EnableTransactionManagement
public class TestConfig {
@Bean
public DataSource dataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("org.hsqldb.jdbcDriver");
    ds.setUrl("jdbc:hsqldb:mem:testdb");
    ds.setUsername("sa");
    ds.setPassword("");
    return ds;

}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){

    LocalContainerEntityManagerFactoryBean lcemfb
        = new LocalContainerEntityManagerFactoryBean();

    lcemfb.setDataSource(this.dataSource());
    lcemfb.setPackagesToScan(new String[] {"com.dao","com.data"});
    lcemfb.setPersistenceUnitName("MyTestPU");

    HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
    lcemfb.setJpaVendorAdapter(va);

    Properties ps = new Properties();
    ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    ps.put("hibernate.hbm2ddl.auto", "create");
    lcemfb.setJpaProperties(ps);

    lcemfb.afterPropertiesSet();

    return lcemfb;

}

@Bean
public PlatformTransactionManager transactionManager() {
    JpaTransactionManager tm = new JpaTransactionManager();
    tm.setEntityManagerFactory(this.entityManagerFactoryBean().getObject());

    return tm;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
    return new PersistenceExceptionTranslationPostProcessor();
}

@Bean
public AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor()
{
    return new AutowiredAnnotationBeanPostProcessor();
}


}

ProductsDaoTest.java

@ContextConfiguration(classes = { TestConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductsDaoTest {

 @Resource(name="com.dao.ProductsDao")
 private ProductsDao testDao;

 @Test
 public void testSaveProduct() {


     Product productA = new Product();
     testDao.save(productA);

     Set<Product> products = testDao.getAllProducts();

     assertNotNull(products);
 }   
}

The error is Error creating bean with name ‘com.dao.ProductsDaoTest’: Injection of resource dependencies failed

So it can’t find the ProductDao Bean which is a @Repository with a @Autowired sessionFactory.

So my guess is that because I’m not naming the beans using xml it can’t find it, though I thought it should automatically pick it up from setPackagesToScan(). So is there a way to manually insert the Bean mapping so that it can be found?

Also more generally is this a reasonable way to go about testing Spring DAO configurations?

Regards,
Iain

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:12:05+00:00Added an answer on June 18, 2026 at 7:12 pm

    I think you are trying to use wrong name of your DAO bean in @Resource annotation. Have you explicitly specify name of the ProductsDao bean using @Qualifier? If no, then as I remember by default the name of the bean will be productsDao. So you should inject your DAO like:

    @Resource(name="productsDao")
    private ProductsDao testDao;
    

    If you have only one ProductDAO implementation then simply write:

    @Autowired
    private ProductsDao testDao;
    

    or

    @Inject
    private ProductsDao testDao;
    

    In case if you want to give specific name to DAO then use next construction:

    @Respository
    @Qualifier(name="specificName")
    public class ProductDAO...
    

    EDIT:

    As Boris noted you should also specify which package to scan for defined beans (classes annotated with @Component, @Service, @Repository…). For this you should add @ComponentScan annotation to your configuration class definition.

    @Configuration
    @EnableTransactionManagement
    @ComponentScan(basePackages = {"package_to_scan"})
    public class TestConfig {...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.