Spring 依赖项未使用 Cucumber 注入

问题描述 投票:0回答:1

< TL;DR >

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

< / TL;DR>

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

堆叠

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

我尝试过的

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

将 Spring 日志级别设置为 DEBUG 几乎没有什么问题,也没有什么值得关注的。对于使用 Cucumber 运行程序 (@RunWith(Cucumber.class)) 的测试,我看到来自 Spring 的日志相对较少。与使用 SpringJunit4Runner 的不相关测试相比,几乎没有。

我编写了一个测试,使用 SpringJunit4Runner 而不是 Cucumber 运行程序并自动装配问题类,它工作得很好;该类不为空。

代码

聚甲醛

<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);
    }
java spring cucumber-jvm
1个回答
8
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.