我试图让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);
}
}
test-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配置和自定义上下文加载器。
发布于 2019-03-30 00:48:35
我用下面的方法解决了这个问题(基本上是我自己注入服务):
@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在context需要重新加载时进行控制(如果我理解正确的话)。
我对这个解决方案并不完全满意,我怀疑它不等同于以标准方式启用SpringClassRule/SpringMethodRule,但它在我的情况下是有效的。
https://stackoverflow.com/questions/54787947
复制相似问题