javaspringjunitjbehaveserenity-bdd

Integrating Spring with Serenity/JBehave test


I am trying to get Spring dependency injection to work inside a Serenity/JBehave test, but neither SpringClassRule nor SpringMethodRule gets applied (which, I suspect, is why both @ContextConfiguration and @Autowired are ignored, and I get a NullPointerException when tyring to call the service).

I've also tried SpringIntegrationClassRule and SpringIntegrationMethodRule from the serenity-spring library, but to no avail.

Does anyone have any idea how to get it working?

My test 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);
    }
}

My story:

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";
    }
}

I use the following libraries:

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

Note: This is a very simplified version of my real-life case, and I need both the Spring XML config and the custom context loader.


Solution

  • I solved this using the following hack (basically injecting the service(s) myself):

    @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 (must be the same package as SpringClassRule to get access to the getTestContextManager method):

    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);
       }
    
    }
    

    I'm getting the Spring context through SpringClassRule so that it gets cached by Spring, letting Spring control when the context needs reloading (if I have understood correctly).

    I'm not entirely satisfied with this solution and I suspect it's not equivalent to enabling SpringClassRule/SpringMethodRule in the standard way, but it works in my case.