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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:49:54+00:00 2026-06-14T23:49:54+00:00

An IllegalStateException gets thrown on attempting to JUnit test a MyBatis-Spring project. The problem

  • 0

An IllegalStateException gets thrown on attempting to JUnit test a MyBatis-Spring project.

The problem appears to be with Autowiring MyBatis Mapper Beans (pardon my jargon since I’m new to the whole MyBatis-Spring-JUnit setup). I inherited the setup from somebody else. Let me know if any other information is relevent to get help.

The setup described below works when I expose the whole thing as a web-service. But, it fails when I try to JUnit test it. How can I Autowire the mapper while testing?

Setup

Under main/java

  • persistence folder: ProductMapper.java
  • service folder: ProductService.java
  • ws folder: BrowserService.java
  • ws/impl folder: BrowserServiceImpl.java

Under main/resources

  • persistence folder: ProductMapper.xml

BrowserService.java

class BrowserServiceImpl {

    private ProductService productService;

    // setters and getters
    // Methods
}

ProductService.java

class ProductService {

    @Autowired
    private ProductMapper productMapper;

    // Methods
}

Context.xml

<beans>
    <bean id="browserSvc" class="com.comp.team.proj.ws.impl.BrowserServiceImpl">
        <property name="productService" ref="productService" />
    </bean>

    <!-- MyBatis Config -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.comp.team.proj.persistence" />
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@xxx.xxx.xxx.com:xxxx:xxx"/>
        <property name="username" value="myusername"/>
        <property name="password" value="mypassword"/>
    </bean>

    <!-- Other beans -->
</beans>

If you are curious what the Exception looks like:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: 
  Could not autowire field: private com.comp.team.proj.persistence.ProductMapper 
    com.comp.team.proj.service.ProductService.productMapper;

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
  No matching bean of type [com.comp.team.proj.persistence.ProductMapper]
    found for dependency:
       expected at least 1 bean which qualifies as autowire candidate for this 
       dependency.
...

EDIT

See the dataSource Bean in the updated Context.xml above. Does the configuration look correct?

Looks like one problem is with the dataSource setup. I make this assumption because the JUnit test runs, but throws an exception. So, it’s not the JUnit setup most likely. If the dataSource setup is correct, MyBatis will successfully instantiate the Mapper Bean and the Autowiring will succeed.

Test File

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductCountTest {

    @Autowired
    protected BrowserService browserSvc;

    @Test
    public void testGetProductCount() {
        long count = browserSvc.getProductCount();

        assertTrue(count > 0);
    }

}

It finds the Context.xml file without any problem as I placed it in the right directory.

  • 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-14T23:49:55+00:00Added an answer on June 14, 2026 at 11:49 pm

    Correct Data-Source config here.

    I discovered that there was another way to do the mapping bean:

    <bean id="productMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="mapperInterface" value="com.comp.team.proj.persistence.ProductMapper" />
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    

    I think the IllegalStateException was thrown because:

    The data-source was incorrectly configured, which led to the mapping to fail resulting in the Mapper bean not being created (NoSuchBeanDefinitionException).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a problem with java.lang.IllegalStateException ... I don't know why it's thrown during
I'm using Spring 3. When controller gets requests it passes control to method someMethod()
Not able to solve this problem Error : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: javax.xml.parsers.FactoryConfigurationError: Provider
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor at android.graphics.Canvas.<init>(Canvas.java:127) at app.test.canvas.StartActivity.applyFrame(StartActivity.java:214) at app.test.canvas.StartActivity$1.onClick(StartActivity.java:163) at
In my servlet an exeception thrown as org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called
In my spring controllers, annotated with aspects, I am attempting to remove CGLib proxies
We have a simple stateless EJB timer that gets an instance of a spring
Sometimes I get IllegalStateException thrown by MediaPlayer.SeekTo(int) The MediaPlayer runs in a Service, and
What does it mean when a IllegalStateException is thrown when entering a synchronized block?
After setting up my Wicket project with Guice Servlet, I get a java.lang.IllegalStateException: filter

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.