[Cucumber] JUnit, Spring and autowiring

We are having trouble with JUnit-tests that are doing dependency
injection in
the setup of database connections. The stacktrace identifies autowiring
of
sessionFactory as the problem. During bean creation of sessionFactory we
get
a NoSuchMethodError; probably because of trouble when initializing the
cglib
class net.sf.cglib.proxy.Enhancer. I am using cucumber-maven-plugin on a
pretty standard Spring project with Spring MVC, Hibernate and Java 1.6,
deploying to Tomcat and Jetty.

– Test Class (autowiring daoImpl):

@ContextConfiguration(locations = {"/test-config-db.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SdbCollectorDbTest {

    @Autowired
    private EventFactory eventFactory;

    @Autowired
    @Qualifier("sdbDaoImpl") // tried using autowiring by name, it made 
no
difference
    protected SdbDao sdbDao;

– DaoImpl (autowiring sessionFactory):

@Repository
@Qualifier("sdbDaoImpl") // tried using autowiring by name, it made no
difference
@Transactional(readOnly = true)
public class SdbDaoImpl implements SdbDao {

    @Autowired
    private SessionFactory sessionFactory;

– Bean-config for sessionFactory and transactionManager:

    <context:component-scan base-package="...cut..."/>
    <tx:annotation-driven/>
    <bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceNMHDB2P"/>
        <property name="mappingResources">
            <list>... cut...</list>
        </property>
        <property name="hibernateProperties">
            ...cut...
        </property>

        <property name="lobHandler" ref="defaultLobHandler"/>
    </bean>

– Stacktrace:
java.lang.IllegalStateException: Failed to load ApplicationContext
at
org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:203)
at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at
org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:93)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:130)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at
com.intellij.rt.junit4.Junit4ClassSuite.run(Junit4ClassSuite.java:99)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: org.springframework.beans.factory.BeanCreationException:
Error
creating bean with name ‘sdbDaoImpl’: Autowiring of fields failed;
nested
exception is org.springframework.beans.factory.BeanCreationException:
Could
not autowire field: private org.hibernate.SessionFactory
.SdbDaoImpl.sessionFactory; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean
with name ‘sessionFactory’ defined in class path resource
[datasource.xml]:
Invocation of init method failed; nested exception is
java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(Z)V
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native
Method)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at
org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
at
org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
at
org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
at
org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:199)
… 22 more
Caused by: org.springframework.beans.factory.BeanCreationException:
Could
not autowire field: private org.hibernate.SessionFactory
.adiseksport.dao.SdbDaoImpl.sessionFactory; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean
with name ‘sessionFactory’ defined in class path resource
[datasource.xml]:
Invocation of init method failed; nested exception is
java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(Z)V
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at
org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
… 39 more
Caused by: org.springframework.beans.factory.BeanCreationException:
Error
creating bean with name ‘sessionFactory’ defined in class path resource
[datasource.xml]: Invocation of init method failed; nested exception is
java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.(Z)V
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native
Method)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
… 41 more
Caused by: java.lang.NoSuchMethodError:
org.objectweb.asm.ClassWriter.(Z)V
at
net.sf.cglib.core.DebuggingClassWriter.(DebuggingClassWriter.java:47)
at
net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
at
net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
at
net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at
net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at
net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
at
net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at
net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69)
at
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:117)
at
org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43)
at
org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)
at
org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:135)
at
org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:55)
at
org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:56)
at
org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:302)
at
org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:434)
at
org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:109)
at
org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at
org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:226)
at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
at
org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
at
org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
at
org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
… 53 more


View this message in context:
http://www.nabble.com/-Cucumber--JUnit%2C-Spring-and-autowiring-tp23595316p23595316.html
Sent from the rspec-users mailing list archive at Nabble.com.

We are having trouble with JUnit-tests that are doing dependency injection in
the setup of database connections. The stacktrace identifies autowiring of
sessionFactory as the problem. During bean creation of sessionFactory we get
a NoSuchMethodError; probably because of trouble when initializing the cglib
class net.sf.cglib.proxy.Enhancer. I am using cucumber-maven-plugin on a

The cucumber-maven plugin is intended to run Cucumber features, not
JUnit tests, so I’m not sure I understand what you’re trying to do.
There is nothing JRuby or Cucumber specific in your stack trace.

Could you please follow the guidelines for contributing and commit a
fork to the cucumber_java project that shows how to reproduce the
problem?
http://wiki.github.com/aslakhellesoy/cucumber/contributing

For new posts, please use the new ML:
http://www.nabble.com/-Cucumber--ANN%3A-New-Google-Group-for-Cucumber-td23602831.html

Aslak