Search code examples
javaintellij-ideacucumbergherkincucumber-java

Unable to create .feature cucumber file IntelliJ - 'Cannot create class-file'


I have a java cucumber project which is set up with all the required dependencies running latest dependencies of cucumber-java, cucumber-junit and gherkin etc. I also have plugins installed for cucumber and gherkin.

IntelliJ isn't allowing me to create a file with the extension '.feature'. I get an error of 'Cannot create class-file'. Oddly, if I create a regular file (i.e. with no .feature extension) in my ./resources/features folder, then associate it as a 'cucumber scenario' file, IntelliJ recognises this and prefixes it with the little green cucumber sign in the file directory. It also recognises all the 'Given When Then' syntax and formats it in the file correctly. But when I run my runner, it reports no features are found. I think this is because it is looking for '.feature' extension?

Has anyone had this issue before? Its baffling me as it appears cucumber and gherkin are set up correctly, given it allows me to create a cucumber scenario file, but just not with the '.feature' extension. For information my runner file looks like the below. Thanks in advance of any help, its driving me mad :)

@RunWith(Cucumber.class)
@CucumberOptions(features = "C:\\Users\\joe.bloggs\\Desktop\\dram_automation_suite\\src\\test\\resources\\features")
public class CucumberRunTest {
}

Solution

  • Your error sounds indeed strange. My experience of creating a file, any file, in Idea just works. Are you sure you are not trying to create a java file, i.e. a class?

    For your other issue, place the feature file in the same package as the test runner lives is. But in the resources directory.

    If your runner lives in the package com.company.lucy then store the feature files in src/test/resources/com/company/lucy This will allow Cucumber to find it since Cucumber searches its classpath and uses every file it finds called .feature

    This will allow you to speciy your runner as:

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

    And your issue with maintaining many feature files will disappear.

    As this seems to be a Maven project, anything placed in the resources folder will end up on your classpath. Anything in src/test/resources will end up on your test classpath.