java - Cucumber with Spring Boot 1.4: Dependencies not injected when using @SpringBootTest and @RunWith(SpringRunner.class) -
i writing new app , trying bdd using cucumber , spring boot 1.4. working code shown below:
@springbootapplication public class application { @bean myservice myservice() { return new myservice(); } public static void main(string[] args) { springapplication.run(application.class, args); } } public class myservice {}
test code shown below:
@runwith(cucumber.class) public class runfeatures {} @runwith(springjunit4classrunner.class) @contextconfiguration(classes = application.class, loader = springapplicationcontextloader.class) public class mystepdef { @autowired myservice myservice; @given("^some initial condition$") public void appisstarted() throws throwable { if (service == null) throw new exception("dependency not injected!"); system.out.println("app started"); } @then("^nothing happens$") public void thereshouldbenoexception() throws throwable { system.out.println("test passed"); } }
feature file shown below:
feature: test cucumber spring scenario: first scenario given initial condition nothing happens
when run above is, works , dependency (myservice) injected mystepdef no issues.
if replace code:
@runwith(springjunit4classrunner.class) @contextconfiguration(classes = application.class, loader = springapplicationcontextloader.class)
with code below (new way handle in spring boot 1.4):
@runwith(springrunner.class) @springboottest
then dependency (myservice) never gets injected. missing perhaps?
thanks in advance help!!!
i had same problem. comment above directed me solution
the problematic code in cucumber-spring seems github.com/cucumber/cucumber-jvm/blob/master/spring/src/main/…
after adding annotation @contextconfiguration
tests working expected.
so i've got following...
@runwith(cucumber.class) @cucumberoptions(plugin = {"json:target/cucumber.json", "pretty"}, features = "src/test/features") public class cucumbertest { } @contextconfiguration @springboottest public abstract class stepdefs { } public class mystepdefs extends stepdefs { @inject service service; @inject repository repository; [...] }
i hope helps further
Comments
Post a Comment