JUnit Test在Local上运行,但在Jenkins上运行

时间:2015-07-31 13:54:17

标签: java spring maven jenkins

我和詹金斯发生了奇怪的错误。

Jenkins服务器与本地测试位于同一台计算机上,但这是我所拥有的:

当我在Local上运行关于名为CarStatusDao的类的4个测试时(通过运行cmd Windows),在输出中输入:

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

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

测试没有任何问题。

当我执行完全相同的代码时,但是在Jenkins上(从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!

从这些日志中,重要的部分是:

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; 

所以基本上,在本地Maven中autowired可以在carStatusDAO类中CarStatusDaoTest我的属性<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 /> ,但是当我在Jenkins上运行它时,它无法...:/

我不明白为什么这样的不同行为,而Maven是相同的,代码也是相同的....:/

我认为这是类路径问题,因为这是唯一不同的事情:/

但我不知道如何解决它。

对于上下文,这是我的/META-INF/spring/carfleet-dao-test-context.xml:

public class CarStatusDaoTest extends AbstractDaoTest {

    @Autowired
    CarStatusDao carStatusDAO;

    @Test
    public void getCurrentStatusOfCarTesting() {

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

    [...]
}

以下是我的类CarStatusDaoTest的定义:

@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);
    }
}

以下是我所有测试的母亲课程:

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"

提前致谢, 最诚挚的问候。

修改

以下是Jenkins上显示的Maven配置:

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"   

以下是Windows控制台中mvn -v的结果:

  public class Sample {
    private int m_Data;

    // property (and "get") is public - anyone can read the property
    public int Data {
      get { // "get" has "public" modifier as property does
        return m_Data; 
      }
      internal set { // "set" is internal - caller should be in the same assembly 
        m_Data = value;
      }
    }
  }

2 个答案:

答案 0 :(得分:1)

这个问题只是班级CarStatusDaoImpl没有被提交......

每个类都在SVN上提交,Jenkins正在接收它们:

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

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

出现在詹金斯身上!谢谢@ nesohc

答案 1 :(得分:0)

当jenkins作业在本地运行时遇到提到的browserCapabilties时,我得到与Bean相关的错误。 ,删除浏览器功能并在本地运行作业后未引发此错误,并通过了测试 enter image description here