简体   繁体   中英

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 (by runnning the cmd Windows), 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.

EDIT

Here is the Maven configuration displayed on Jenkins :

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
Maven home: C:\Program Files\Maven
Java version: 1.7.0_80-ea, vendor: Oracle Corporation
Java home: C:\Java\jdk1.7.0_80\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

And here is the result of mvn -v in the Windows console :

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
Maven home: C:\Program Files\Maven
Java version: 1.7.0_80-ea, vendor: Oracle Corporation
Java home: C:\Java\jdk1.7.0_80\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"   

It was just an issue that the class CarStatusDaoImpl was not commited...

With every class commited on the SVN, Jenkins is getting them and :

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

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

Is appearing on Jenkins ! Thanks @ nesohc !

i get this bean related error when jenkins job had browserCapabilties mentioned while run local. ,after deleting browserCapbilties and run the job local did not throw this error and test passed 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM