簡體   English   中英

將Spring與Serenity / JBehave測試集成

[英]Integrating Spring with Serenity/JBehave test

我正在嘗試讓Spring依賴項注入在Serenity / JBehave測試中工作,但是沒有應用SpringClassRule和SpringMethodRule(我懷疑這就是為什么@ContextConfiguration和@Autowired都被忽略的原因,並且在調用時我得到了NullPointerException服務)。

我還嘗試了來自serenity-spring庫的SpringIntegrationClassRule和SpringIntegrationMethodRule,但無濟於事。

有誰知道如何使它工作?

我的測試課:

@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配置和自定義上下文加載器。

我使用以下技巧(基本上是自己注入服務)解決了這個問題:

@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,但是在我的情況下它可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM