gpt4 book ai didi

java - JUnit 测试在 Local 上运行,但不在 Jenkins 上运行

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:52 25 4
gpt4 key购买 nike

我在使用 Jenkins 时遇到了奇怪的错误。

Jenkins 服务器与本地测试在同一台计算机上,但这是我得到的:

当我在本地(通过运行 cmd Windows)对名为 CarStatusDao 的类运行 4 次测试时,输出如下:

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 中,但是当我在 Jenkins 上运行它时,它不能...:/

我不明白为什么会有如此不同的行为,而 Maven 是相同的,代码也是相同的......:/

我认为这是一个类路径问题,因为这是唯一不同的地方:/

但是我不知道怎么解决。

对于上下文,这是我的/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 />

这是我的类 CarStatusDaoTest 的定义:

public class CarStatusDaoTest extends AbstractDaoTest {

@Autowired
CarStatusDao carStatusDAO;

@Test
public void getCurrentStatusOfCarTesting() {

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

[...]
}

这是我所有测试的母类:

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

提前致谢,最好的问候。

编辑

这是在 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 的结果:

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"

最佳答案

这只是一个问题,类 CarStatusDaoImpl 没有提交......

随着在 SVN 上提交的每个类,Jenkins 正在获取它们并且:

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

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

正在出现在 Jenkins 上!谢谢@nesohc !

关于java - JUnit 测试在 Local 上运行,但不在 Jenkins 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31747932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com