0

I'm able to initiate Spring when i'm debbuging StepDefinitions.java, but running the test from gradle produces null. Do I need an aditional glue?

Produces null: gradle cucumber

Produces null: running myFeature.features

Produces myService (working): running Stepdefinitions.java

I have tried the following code:

@ContextConfiguration(
        classes = Application.class,
        loader = SpringBootContextLoader.class)
@WebAppConfiguration

Current StepDefinitions:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class StepDefinitions{
    private static String roll;

   @Autowired
   MyService myService;


    /** Reset all static values before running test cases    */
    @BeforeClass
    public static void resetValues() {
        roll = "";
    }

    //I use swedish, "Given"
    @Givet("Jag har rollen {string}")
    public void getRole(String roll) {
        assertNotNull(roll);
        this.roll = roll;
        myService.useRole(roll);
    }
}

gradle.build:

dependencies {
compile files('../libs/cucumber-spring-4.7.1.jar')
testCompile 'io.cucumber:cucumber-java:' + cucumberVersion
testCompile 'io.cucumber:cucumber-junit:' + cucumberVersion
...
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + 
sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'steps/rufs', 
'src/test/resources/features', '--tags','@rufs']
            }
    }
}
Softy_
  • 1
  • 1
  • 1
    You are not getting JUnit involved anywhere. `RunWith` is used by JUnit, and this is what prompts Spring to get involved. When Cucumber is running as your suite, it's ignoring those annotations. – Michael Aug 19 '19 at 07:51
  • Have a look at: https://github.com/cucumber/cucumber-jvm/tree/master/examples/spring-txn – M.P. Korstanje Aug 19 '19 at 07:54

1 Answers1

0

You are not getting JUnit involved anywhere when running from Gradle. @RunWith is used by JUnit, and this in turn is what prompts Spring to get involved. When Cucumber is running as your suite, it's ignoring those annotations because it doesn't understand them.

You'll need to use JUnit as your suite (i.e. not run cucumber.api.cli.Main). You then have a problem because you need to use two "runners": Cucumber and Spring.

The way around this is JUnit's "rules" for one of the runners. Spring has a Rule, but as far as I can see Cucumber does not. In this case, use the Cucumber runner:

@RunWith(Cucumber.class)

in combination with Spring's rules, as described here: How to run JUnit SpringJUnit4ClassRunner with Parametrized?

Michael
  • 41,989
  • 11
  • 82
  • 128
  • @mpkorstanje `@RunWith` is a JUnit annotation, which he is already using in his code. If you want Spring integration with Cucumber then JUnit needs to be your suite. This is why when he runs the test from his IDE (i.e. with JUnit suite), it's working, but why from Gradle (Cucumber suite) it doesn't. – Michael Aug 19 '19 at 08:01