首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Spring与Serenity/JBehave测试集成

将Spring与Serenity/JBehave测试集成
EN

Stack Overflow用户
提问于 2019-02-20 21:50:57
回答 1查看 432关注 0票数 0

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

我还尝试了来自serenity-spring库的SpringIntegrationClassRule和SpringIntegrationMethodRule,但都没有用。

有谁知道怎么让它工作吗?

我的测试类:

代码语言:javascript
运行
复制
@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);
    }
}

我的故事:

代码语言:javascript
运行
复制
Scenario: Hello world
When I want a greeting
Then I shall be greeted with "Hello world"

TestContextLoader.java:

代码语言:javascript
运行
复制
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);
    }

}

test-beans.xml:

代码语言:javascript
运行
复制
<?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:

代码语言:javascript
运行
复制
@Service
public class GreetingService {

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

我使用以下库:

代码语言:javascript
运行
复制
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配置和自定义上下文加载器。

EN

Stack Overflow用户

回答已采纳

发布于 2019-03-30 00:48:35

我用下面的方法解决了这个问题(基本上是我自己注入服务):

代码语言:javascript
运行
复制
@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方法):

代码语言:javascript
运行
复制
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在context需要重新加载时进行控制(如果我理解正确的话)。

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

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54787947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档