1

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.

mauryat
  • 1,610
  • 5
  • 29
  • 53
  • 1
    where is your testing code..you need to configure autowiring from the testing class also..and you need to use annotations such as runwithjunit4class and contextconfiguration("locationof dispatcherservlet.xml"). – Pratap M Oct 30 '12 at 06:56
  • if you are doing testing for a class in which autowiring is used you should use the codes suggested by pawelccb..it is usual error if you do not give those annotations given by pawelccb..the thing is that testing class does not know the autowiring issues if we do not give the path of dispatcherservlet.xml in which component-scan is key for recognizing the autowiring classes.. – Pratap M Oct 30 '12 at 07:16

2 Answers2

1

Please provide us the code of unit test.

I suppose that you are missing spring configuration

@ContextConfiguration(locations = { 
        "classpath:/com/comp/team/proj/context.xml" // give the correct path 
        })
@RunWith(SpringJUnit4ClassRunner.class)
public class BrowserServiceTest {

@Autowired
BrowserServiceImpl browserService;

@Test
public void shouldTestSmth(){

}

Remember that during testing Spring uses spring-test lib.

EDIT: I suppose that the most important part is still not shown. Do you have bean definition of ProductMapper in your context.xml? I suppose that it's missing.

I can also see that you're mixing annotations and bean definitions. Maybe you need bean definition?

pawelccb
  • 515
  • 6
  • 11
0

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

Community
  • 1
  • 1
mauryat
  • 1,610
  • 5
  • 29
  • 53