Search code examples
javacucumbercommand-line-interfaceprogram-entry-pointcucumber-jvm

How to invoke the Cucumber runner class from a different main method


I'm new to using Command Line Interface. So I just have a question on how to invoke the runner class of the cucumber using CLI technique.

I have a Java program which contains a main method. When testers pass the argument which is test case, it will fetch the feature file. The java program invoke a custom made API which will fetch the correct feature file.

Next I'll have to invoke the Cucumber runner class to execute the test case. I need to pass this particular feature file as the argument. Two questions, Can we invoke the runner class from a different main method. I did some research and I was not able to find a concrete answer. Two questions,

  1. cucumber.api.cli.Main.main(arguments); So how do i specify the jar location of my runner class.

    `FeatureFileCreation.main("xxxxx"); - API that fetches the right feature file String[] arguments = {"-", ""}; cucumber.api.cli.Main.main(arguments);

    • How do I specify where my jar is located? How can I pass my feature file?`
  2. Should I create a main method in the runner class, something like this? For the sake of using CLI,Since I need to create a runnable jar. I should have a main method in my runner class.

`

@RunWith(Cucumber.class)
@Cucumber.Options(features="C:/Users/IBM_ADMIN/Desktop/CRAutomation/CR Regression1/src/My.feature",glue={"bell.canada.step.definition"})

public class AutomationRunnerAction { 

    public void main(){
    }
}`

Please note that, Getting the right feature file is 1 java API. I will invoking that API from one main method of one java program. The runner class with step definition and methods are a diff java program.


Solution

  • Unfortunately the accept answer is not correct. If you look at the source of Main.main() you'll notice that it contains: System.exit(exitstatus) which terminates the system.

    The proper way to run the commandline programatically would be to use Main.run() like this:

    String [] argv = new String[]{ "-g","","./src/main/java/featureDetails/Testing.feature"};
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    byte exitstatus = Main.run(argv, contextClassLoader);