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 6747881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:29:55+00:00 2026-05-26T12:29:55+00:00

I use mockito as a mock object library. I am unit testing DAOs. DAOs

  • 0

I use mockito as a mock object library. I am unit testing DAOs.

DAOs expect JdbcTemplate to be injected through @Autowired. Hence, there are no setter methods for JDBC Template in DAOs which unit tests can call.

I have the following test spring application context:

<b:beans 
    xmlns:b="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:annotation-config />

    <b:bean id="mockito" class="org.mockito.Mockito" />
    <b:bean 
        id="mockJdbcTemplate"
        factory-bean="mockito"
        factory-method="mock">
        <b:constructor-arg value="org.springframework.jdbc.core.JdbcTemplate"/>
    </b:bean>
</b:beans>

I was expecting that at test execution time, spring will create mock jdbctemplate instance and autowire it to DAO.

But that doesn’t happen – instead I just get the following exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.jdbc.core.JdbcTemplate] 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:920)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 42 more

Has anyone had success with this approach before?

Thanks.

  • 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-05-26T12:29:57+00:00Added an answer on May 26, 2026 at 12:29 pm

    I think the problem is that the factory method mock which returns generic type. After erasure, the spring is not able to deduct object type, try to provide explicit object type by class attribute.

    public MockitoMockFactoryBean implements FactoryBean<?>
    {
        private Class<?> objectType;
    
        @Override public Object getObject()
        {
            return Mockito.mock(objectType);
        }
        public void setObjectType(Class<?> objectType)
        {
            this.objectType=objectType;
        }
        @Override public Class<?> getObjectType()
        {
            return objectType;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Mockito. Before starting to use mock objects I had some Unit tests
I'm using Mockito for my unit testing, and I've run across an issue where
I'm new to Mockito and want to use it in unit tests. What I
Im learning Mockito and in chapter 16 they say you should not use partial
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Can I use Mockito to capture what was passed to the HttpServletResponse#sendError() method? I
Use case: Fitnesse is used for automated testing of the web site. SUT (software
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I'm trying to set up and use Mockito into a GWT project, and I'm
Trying to use NUnit to test a method that adds an object to a

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.