vendredi 31 juillet 2015

JUnit Test runs on Local but not on Jenkins

I've got strange error with Jenkins.

The Jenkins server is on the same computer as the local test, but here is what I've got :

When I run 4 tests about a class named CarStatusDao on Local, here in the output :

Running net.****.****.dao.carstatus.CarStatusDaoTest

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

The tests run without any problem.

When I execute the exact same code, but on Jenkins (who get the exact same code from a SVN):

Running net.****.****.dao.carstatus.CarStatusDaoTest

2015-07-31 15:29:21,497 ERROR [org.springframework.test.context.TestContextManager] - Caught exception while allowing TestExecutionListener 
[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@c316b9] to prepare test instance [net.****.****.dao.carstatus.CarStatusDaoTest@1121079]
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'net.****.****.dao.carstatus.CarStatusDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: net.****.****.dao.CarStatusDao net.****.****.dao.carstatus.CarStatusDaoTest.carStatusDAO; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No matching bean of type [net.****.****.dao.CarStatusDao] 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
    at [...]

Tests run: 4, Failures: 0, Errors: 4, Skipped: 0, Time elapsed: 0.011 sec <<< FAILURE!

From theses logs, the important part is :

Error creating bean with name 'net.****.****.dao.carstatus.CarStatusDaoTest': 
Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: net.****.****.dao.CarStatusDao net.****.****.dao.carstatus.CarStatusDaoTest.carStatusDAO; 

So basically, in local Maven is able to autowired my attribute carStatusDAO in the class CarStatusDaoTest, but when I run it on Jenkins, it is not able... :/

I don't understand why such differents behaviors whereas the Maven is the same and the code is the same also.... :/

I assume it's a classpath problem, because that's the only different thing :/

but I don't know how to fix it.

For the context, here is my /META-INF/spring/carfleet-dao-test-context.xml :

<context:component-scan base-package="net.****.****" />

<jd:embedded-database id="dataSource" type="HSQL">
    <jd:script location="classpath:sql/hsql-schema.sql" />
    <jd:script location="classpath:sql/test-data.sql" />
</jd:embedded-database>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="persistenceUnitName" value="testunit" />
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" >
        <list>
            <value>net.****.****.domain</value>
        </list>
    </property>
</bean>

<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />

Here is the definition of my class CarStatusDaoTest :

public class CarStatusDaoTest extends AbstractDaoTest {

    @Autowired
    CarStatusDao carStatusDAO;

    @Test
    public void getCurrentStatusOfCarTesting() {

        carStatus = carStatusDAO.getCurrentStatusOfCar(-1L);
        assertEquals(carStatus, null);
    }

    [...]
}

And here is the Mother Class for all my tests :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:/META-INF/spring/carfleet-dao-test-context.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class AbstractDaoTest {

    @Autowired
    private EntityFactory entityFactory;

    public EntityFactory getEntityFactory() {
        return entityFactory;
    }

    @Test
    public void shouldEntityFactoryBeNotNull() {

        assertNotNull(entityFactory);
    }
}

Thanks in advance, Best Regards.

Aucun commentaire:

Enregistrer un commentaire