java - 没有注入(inject) Cucumber 的 Spring 依赖项

标签 java spring cucumber-jvm

< 长话短说 >

问题: 我正在使用 Cucumber-JVM 和 Spring 进行集成测试。测试步骤类中的 Autowiring 类未创建且为空。

/TL;DR>

测试在本地工作但在构建服务器上失败,当尝试对 Autowiring 的 bean 进行方法调用时出现空指针。

堆栈

  • Java 1.8(本地和构建服务器)
  • Maven 3.3.9(本地和构建服务器)
  • 本地:Windows 8,构建服务器:Ubuntu(无法理解这有什么不同)

我尝试过的

问题类被注释为@Component,我尝试删除@Component 注释并在 spring 上下文中注册它——这没有效果。

将 Spring 日志级别设置为 DEBUG 几乎没有任何问题。对于使用 Cucumber runner (@RunWith(Cucumber.class)) 的测试,我看到来自 Spring 的日志相对较少。与使用 SpringJunit4Runner 的无关测试相比,几乎没有。

我编写了一个测试,使用 SpringJunit4Runner 而不是 Cucumber runner 并 Autowiring 问题类,它运行良好;该类不为空。

代码

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

最佳答案

我讨厌回答自己的问题,但我找到了解决方案。

  1. 将 cucumber(所有 cucumber info.cukes 依赖项)升级到 1.2.4
  2. @ContextConfiguration("classpath:*Cucumber.xml") 添加到我所有的 Step def 类中

升级后,我在调试级别看到了预期数量的 Spring 日志记录,用于使用 Cucumber runner 运行的测试。

关于java - 没有注入(inject) Cucumber 的 Spring 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33945845/

相关文章:

Spring Boot 无法在 ElasticBeanstalk AWS 上找到 jsp 文件

cucumber - 在 Cucumber 场景中将空格作为参数表的值

geb - 在 Geb 中设置请求头和用户代理

java - JMH 状态类和共享状态与非共享状态

java - Chromecast 扩展 Controller 、轨道选择器对话框在 11.0.2 更新后使应用程序崩溃

java - 在 v2 map 上绘制不同颜色的折线

java - 主页RequestMapping。 Spring MVC

java - 保存和加载使用过的图像

java - 使用 javax.sql.Datasource 并在 Websphere 上托管应用程序时出现 IllegalArgumentException

api - 无法在 Eclipse 中将 Karate 编写的功能文件作为 cucumber 功能运行