< TL; DR>
问题: 我正在使用Cucumber-JVM和Spring进行集成测试。未创建测试步骤类中的自动装配类,并且该类为空。
< / TL; DR>
测试在本地工作但在构建服务器上失败,在尝试对自动装配的bean进行方法调用时使用空指针。
堆栈
我尝试了什么
问题类是带注释的@Component,我尝试删除@Component注释并在spring上下文中注册 - 这没有效果。
将Spring日志级别设置为DEBUG几乎没有任何问题。对于使用Cucumber运行程序(@RunWith(Cucumber.class))的测试,我看到来自Spring的相对较少的日志。与使用SpringJunit4Runner的无关测试相比,几乎没有。
我写了一个测试,它使用SpringJunit4Runner而不是Cucumber跑步者并自动测试问题类,它运行良好;这个班不是空的。
代码
POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.foo</groupId>
<artifactId>matching-engine</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>
<name>core</name>
<description>core matching engine</description>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.186</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13 </version>
</dependency>
</dependencies>
</project>
Cucumber Spring上下文(Cucumber.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.foo.matching" />
<context:annotation-config/>
<import resource="matching-engine-spring-context-TEST.xml" />
</beans>
测试中使用的Spring上下文(matching-engine-spring-context-TEST.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- Added this when I removed @Component from its class definition -->
<bean id="testHelper" class="com.foo.matching.test.common.TestHelper"/>
<bean id="orderTimeArrivalService" class="com.foo.matching.orderbook.MockOrderArrivalTimeService"/>
<bean id="tradeExecutionService" class="com.foo.matching.execution.MockTradeExecutionService"/>
<bean id="orderBookService" class="com.foo.matching.orderbook.TestOrderBookService"/>
</beans>
这在构建服务器上不起作用(本地很好):
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/cucumber"})
public class MarketOrderTest {
}
public class MarketOrderSteps {
@Autowired
private TestHelper testHelper;
@Given("^The order book looks like this before the trade is placed:$")
public void setupOrderBook(List<LimitOrder> orders) {
System.out.println("TestHelper: " + testHelper);
testHelper.setupOrderBook(orders);
}
这在构建服务器和本地工作正常,这让我相信问题出在Cucumber /我配置它的方式。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:*Cucumber.xml")
public class SpringTest{
@Autowired
private TestHelper testHelper;
@Test
public void test() {
assertNotNull(testHelper);
}
答案 0 :(得分:6)
我讨厌回答我自己的问题,但我找到了解决方案。
在升级之后,我看到在Cucumber运行程序运行的测试中,调试级别的Spring日志记录量是预期的。