将 Spring 与 Serenity/JBehave 测试集成

Integrating Spring with Serenity/JBehave test

我试图让 Spring 依赖注入在 Serenity/JBehave 测试中工作,但是 SpringClassRule 和 SpringMethodRule 都没有被应用(我怀疑,这就是为什么 @ContextConfiguration 和 @Autowired 都被忽略的原因,并且我在尝试调用服务时收到 NullPointerException。

我还尝试了来自 serenity-spring 库的 SpringIntegrationClassRule 和 SpringIntegrationMethodRule,但无济于事。

有人知道如何让它工作吗?

我的测试class:

@ContextConfiguration(locations = "test-beans.xml", 
                      loader = TestContextLoader.class)
public class GreetingServiceTest extends SerenityStories {

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Autowired
    private GreetingService greetingService;

    private String greeting;

    @When("I want a greeting")
    public void whenIWantAGreeting() {
        greeting = greetingService.getGreeting();
    }

    @Then("I shall be greeted with \"$greeting\"")
    public void thenIShallBeGreetedWith(String greeting) {
        assertEquals(greeting, this.greeting);
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(), this);
    }
}

我的故事:

Scenario: Hello world
When I want a greeting
Then I shall be greeted with "Hello world"

TestContextLoader.java:

public class TestContextLoader implements ContextLoader {

    @Override
    public String[] processLocations(Class<?> clazz, String... locations) {
        return locations;
    }

    @Override
    public ApplicationContext loadContext(String... locations) throws Exception {
        System.err.println("This is never printed.");
        return new ClassPathXmlApplicationContext(locations);
    }

}

测试-beans.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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

    <context:component-scan base-package="com.example"/>

</beans>

GreetingService.java:

@Service
public class GreetingService {

    public String getGreeting() {
        return "Hello world";
    }
}

我使用以下库:

org.springframework:spring-core:5.1.4.RELEASE  
org.springframework:spring-context:5.1.4.RELEASE  
org.springframework:spring-test:5.1.4.RELEASE  
net.serenity-bdd:serenity-core:2.0.40  
net.serenity-bdd:serenity-jbehave:1.44.0  
junit:junit:4.12

注意:这是我真实案例的一个非常简化的版本,我需要 Spring XML 配置和自定义上下文加载器。

我使用以下 hack 解决了这个问题(基本上是我自己注入服务):

@ContextConfiguration(locations = "test-beans.xml", 
                      loader = TestContextLoader.class)
public class GreetingServiceTest extends SerenityStories {

    @Autowired
    private GreetingService greetingService;

    private String greeting;

    @When("I want a greeting")
    public void whenIWantAGreeting() {
        greeting = greetingService.getGreeting();
    }

    @Then("I shall be greeted with \"$greeting\"")
    public void thenIShallBeGreetedWith(String greeting) {
        assertEquals(greeting, this.greeting);
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(), this);
    }

    @BeforeStories
    public final void beforeStories() {
        AutowireCapableBeanFactory beanFactory = getContext().getAutowireCapableBeanFactory();
        beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
        beanFactory.initializeBean(this, getClass().getName());
    }

    private ApplicationContext getContext() {
        return SpringClassRuleHack.getTestContextManager(getClass())
            .getTestContext()
            .getApplicationContext();
    }
}

SpringClassRuleHack(必须与 SpringClassRule 相同的包才能访问 getTestContextManager 方法):

package org.springframework.test.context.junit4.rules;

import org.springframework.test.context.TestContextManager;

public final class SpringClassRuleHack {

   private SpringClassRuleHack() {}

   public static TestContextManager getTestContextManager(Class<?> testClass) {
      return SpringClassRule.getTestContextManager(testClass);
   }

}

我正在通过 SpringClassRule 获取 Spring 上下文,以便它被 Spring 缓存,让 Spring 控制何时需要重新加载上下文(如果我理解正确)。

我对这个解决方案并不完全满意,我怀疑它不等同于以标准方式启用 SpringClassRule/SpringMethodRule,但它适用于我的情况。