Spring addicts not injected with cucumber

<TL; DR>

Problem: I use Cucumber-JVM and Spring for integration tests. The autwired class in the test step class is not created and is null.

</ TL; DR>

Tests are performed locally, but are not executed on the build server with a null pointer when trying to call a method using a bean.

Stack

  • Java 1.8 (local server and build server)
  • Maven 3.3.9 (local server and build server)
  • Local: Windows 8, Build Server: Ubuntu (cannot understand what it matters)

What i tried

The problem class is annotated by @Component, I tried to remove the @Component annotation and register it in the Spring context - this did not affect.

Setting the Spring log level to DEBUG showed very little and didn't bother anything. For tests that use the Cucumber runner (@RunWith (Cucumber.class)), I see relatively small logs from Spring. Almost no one compared to the unrelated tests that use SpringJunit4Runner.

I wrote a test that uses SpringJunit4Runner instead of the Cucumber runner and auto-fixes the problem class, it worked fine; the class was not null.

code

P

<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> 

Spring Cucumber Context (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 context used in tests (match-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> 

This does not work on the build server (normally locally):

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

This works fine on the build server and locally, making me believe that the problem lies somewhere with Cucumber / the way I configured it.

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:*Cucumber.xml") public class SpringTest{ @Autowired private TestHelper testHelper; @Test public void test() { assertNotNull(testHelper); } 
+5
source share
1 answer

I hate answering my questions, but I found a solution.

  • Update the cucumber (all cucumber info.cukes dependencies) to 1.2.4
  • Add @ContextConfiguration ("classpath: * Cucumber.xml") to all Step class classes

After the upgrade, I see the expected amount of Spring logging at the debug level for tests running with the Cucumber runner.

+6
source

Source: https://habr.com/ru/post/1236925/


All Articles