我试图让Spring依赖注入在Serenity / JBehave测试中工作,但是SpringClassRule和SpringMethodRule都没有得到应用(我怀疑,这是为什么@ContextConfiguration和@Autowired都被忽略了,并且当调用tyring时我得到一个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"
test context loader.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>
greeting service.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,但它适用于我的情况。