23

I try to build my first executable specifications with Java and Maven. I created a simple project with this structure:

specification
|-src
  |-test
    |-java
      |-mypackage
        |-MyFeatureTest.java
    |-resources
      |-MyFeature.feature

In the junit test MyFeatureTest.java I have this:

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
public class HomepageTest {
}

Now https://github.com/cucumber/cucumber-jvm/wiki/IDE-support says that I should add the following line:

@Cucumber.Options(paths={"my/super.feature:34"})

I tried to modify that to

@Cucumber.Options(paths={"src/test/resources/"})

but the annotation @Cucumber.Options isn't available at all. My pom.xml has this dependencies:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>1.0.0.RC20</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>1.0.0.RC20</version>
  <scope>test</scope>
</dependency>

Am I missing something?

Update I was missing something: The cucumber feature file has to be in a subdirectory src/test/resources/mypackage/. Otherwise it won't be picked up by the junit test.

I can run my feature tests when I put them in the same directory src/main/test/, so it's not a blocker for me. But I'd like to understand the whole setup.

Sled
  • 18,541
  • 27
  • 119
  • 168
cringe
  • 13,401
  • 15
  • 69
  • 102

9 Answers9

20

Take a look at my question here:

You can specify a location on the classpath by setting the feature attribute in the options annotation like

@Cucumber.Options(features="src/test/resources")

Edit:

in new versions code is

@CucumberOptions(features="src/test/resources")
Community
  • 1
  • 1
lanoxx
  • 12,249
  • 13
  • 87
  • 142
19

The classpath option is not obvious in the Cucumber documentation (it is not in the JavaDoc either), I ended up inferring it from the CLI documentation (edit: link is dead, can't find an equivalent), which has other location options documented. See the List configuration options section in the docs. It is also not obvious how to get the feature defintions from another module of a multi module Maven projectg.

This is what got me going (running from IDE and command line) in a Maven multi module project.

@CucumberOptions(
        features = {"classpath:product"},
        //...
)
public class RunCukesTest extends AbstractTestNGSpringContextTests {

where my feature files were located in

main-project
    sub-module-1
        src/test/java/com/foo/
            RunCukesTest.java
        src/test/resources/product/
            feature_1.feature
            feature_2.feature
    sub-module-2
        ...

It pleases me not to see src/test/resources in the path. Note that there is no leading / in the path. Using the classpath is less brittle, because the classpath (rather than the current working directory) must be well defined.

markdsievers
  • 7,151
  • 11
  • 51
  • 83
  • 6
    This is the correct solution, all the other answers specifying 'src/test/resources' are wrong in the sense, they are brittle will only ever run correctly within an IDE. The use of 'classpath:features' ensure this will work correctly when deployed to a CI build environment. – Martin Spamer Jun 21 '18 at 17:42
  • 1
    Thanks for this solution! I suspect the accepted answer is not actually using the classpath at all. In my case we are packaging the tests in a spring-boot application. I'm hoping that using the classpath means the features can be packaged into the executable jar. – John Mercier Sep 11 '19 at 14:31
7

You can use

@CucumberOptions(
    format = "pretty",
    tags = {"~@Ignore"},
    features = "src/test/resources/com/"  //refer to Feature file
)

for scan all of feature file in package

Jewel
  • 363
  • 4
  • 9
  • good suggestion since @Cucumber.Options is deprecated from version 1.1.5 ... http://xebee.xebia.in/index.php/2014/07/31/cucumber-jvm-all-you-need-to-know-about-cucumberoptions/ – Cris Rockwell May 24 '15 at 18:48
  • Thank you! Was trying @Cucumber.Options which is deprecated – Charlie S May 26 '15 at 14:30
  • Another thanks for showing the new @CucumberOptions! – Brenda Holloway Jul 29 '15 at 15:58
  • 1
    How a knowledgebase remains beneficial days after it is created! After about an year, I get stuck with cucumber-scala project of mine (why not enough documentations around?), search frantically and finally reach here to learn that 'Options' is replaced with 'CucumberOptions'! Great stuff. Thanks. – Nirmalya Aug 19 '15 at 02:03
5

Ok, I can only put this on monday morning... The directory layout I used wasn't correct, I forgot to put the cucumber features into subdirectories matching my package structure.

Make sure you create the needed package directories also in src/test/resources/!

cringe
  • 13,401
  • 15
  • 69
  • 102
1

Once you use

import cucumber.api.CucumberOptions;

You will need to add the following to your pom.xml, otherwise "mvn test" will not work. And you can only run your tests from the IDE. See: https://github.com/cucumber/cucumber-java-skeleton/blob/master/pom.xml

<properties>
    <java.version>1.7</java.version>
    <junit.version>4.12</junit.version>
    <cucumber.version>1.2.2</cucumber.version>
    <maven.compiler.version>3.3</maven.compiler.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <compilerArgument>-Werror</compilerArgument>
            </configuration>
        </plugin>
    </plugins>
</build>
Robert Altena
  • 787
  • 2
  • 11
  • 26
1

Or you could just build your cucumberoptions arguments, like in https://cucumber.io/docs/reference/jvm#cli-runner described and pass it to cucumber.api.cli.Main.run(). This is a small example:

String arg = "classpath:MyFeature.feature --dry-run";
String[] args = arg.split(" ");
cucumber.api.cli.Main.run(args, Thread.currentThread().getContextClassLoader());

and use it in you JUnit Test. So you don't have to create a seperate class for each testclass just with other arguments.

user24502
  • 1,662
  • 4
  • 16
  • 21
0

if you are using JUnit-5 and cucumber latest (7.10) version you need to use this

org.junit.platform.suite.api.SelectDirectories

E.g. @SelectDirectories("src/test/resources")

complete set of runner will look like this.

@ExtendWith(SpringExtension.class)
@CucumberContextConfiguration
@SpringBootTest
@IncludeEngines("cucumber")
@SelectDirectories("src/test/resources")
@SelectClasspathResource("yourclasspathifany")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.abc.xyz")
public class CucumberRunnerTests {}
Himeshgiri gosvami
  • 2,559
  • 4
  • 16
  • 28
0

Alternatively you can solve this problem by adding below code snippet into your pom.xml

<build>
    <testResources>
        <testResource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </testResource>
    </testResources>
</build>
-11

By putting the feature file under src/test/java where the runner and steps file or by putting it under src/main/java the problem will get resolved.

Don't forget to say thanks.:)