24

I have to execute Unit test on one of my Dao classes using Spring. Here is my unit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:app-config.xml"})
@ActiveProfiles("local")
public class HouseDaoTest {

    @Autowired
    HouseDataDao houseDataDao;

    @Test
    public void saveTest(){
        HouseData data = new HouseData();
        Address add = new Address();
        // Truncating for sake of simplicity
        houseDataDao.save(data);
    }
}

And My bean configuration files:

app-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="BeanConfiguration-localhost.xml"/>
    <import resource="BeanConfiguration-production.xml"/>
</beans>

BeanConfiguration-localhost.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans profile="local" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <context:annotation-config />
    <tx:annotation-driven />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/dbtest" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <!-- Tuncated for sake of simplicity -->
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- Project Specific Beans -->
    <bean id="HouseDataDao" class="com.datasaver.dao.HouseDataDaoImpl"></bean>
</beans>

Where BeanConfiguration-production.xml is same as above except for <beans profile="production" ... in it.

When I simply execute a maven test doing mvn test it fails throwing the following exception:

java.lang.IllegalStateException: Could not load TestContextBootstrapper [class org.springframework.test.context.support.DefaultTestContextBootstrapper]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:87)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:102)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:124)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:115)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:81)
    ... 25 more

It looks like I am missing a dependency or something, because the stacktrace contains Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass.

Here is the Spring's dependency list in my pom.xml:

 ...
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
...
Paulo Merson
  • 9,709
  • 5
  • 63
  • 56
Tariq
  • 2,229
  • 9
  • 28
  • 59

3 Answers3

32

It is probably a version conflict since you are using a old version of base spring (2.5.6) with very new one (4.1.4.RELEASE) for your test en context includes

Maarten Seghers
  • 421
  • 3
  • 3
  • 5
    It is certainly a version conflict. `TestContextBootstrapper` was introduced in Spring Framework 4.1. Thus `spring-test` 4.1 requires version 4.1 of `spring-beans`, `spring-core`, etc. In general the version of `spring-test` used should match the version of all other modules from the core Spring Framework. – Sam Brannen Jan 20 '15 at 18:03
  • 1
    i love you (in a developer-2-developer way) – Jaroslav Záruba Sep 16 '17 at 21:20
  • Keep the version of Spring-Context, Spring-Core, Spring-test same. For me it is 4.1.6 for all 3, and this solved my version conflict. – Shubham Pandey Feb 09 '19 at 14:05
2

Maarten is correct. Here is my new list of dependencies in pom.xml which worked for me:

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.5.4-Final</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
</dependencies>
Paulo Merson
  • 9,709
  • 5
  • 63
  • 56
Tariq
  • 2,229
  • 9
  • 28
  • 59
  • You really should **not** be mixing Spring Framework 2.5.6 modules with `spring-test` 4.1, because that is basically guaranteed _not_ to work. – Sam Brannen Jan 20 '15 at 18:04
2

In my case it was a version conflict caused by activemq-all. That dependency (5.12.2 in my case) includes an incompatible version of spring (I just upgraded to spring 4.3.4). So, save yourself a few hours of debugging and check not only the Dependency Hierarchy in your favorite IDE, but also look inside those jar files to see if any are embedding org.springframework packages.

lmyers
  • 2,614
  • 1
  • 22
  • 22