Running Cucumber and Spring boot in Main class

1.7k Views Asked by At

My requirement is to run Cucumber test cases using Spring boot to run through a Custom java main class. I am able to run Cucumber test suite fine if i am using following config class:-

@RunWith(Cucumber.class)
@CucumberOptions(
       plugin = {
            "html:target/cucumber-html-report",
            "json:target/cucumber.json", "pretty:target/cucumber-
             pretty.txt",
            "usage:target/cucumber-usage.json", "junit:target/cucumber-
            results.xml" },
            features = { "src/test/resources" },
            tags = {"@passed"},
            glue =  "cucumberTest.steps")

 public class RunGwMLCompareTests {

 }

And following class to load

   @RunWith(SpringRunner.class)
   @ActiveProfiles("dev")
   @SpringBootTest(classes = Application.class)
   public class AbstractDefinitions {
         public AbstractDefinitions() {
          }
    }

And when i run RunGwMLCompareTests class,Now using this config my Cucumber test cases are running , It loads my Spring boot context and then exceutes all cases defined in feature.

Now my issue is that i want to run this from seperate main java class. I have created my java class as follows :-

package cucumberTest;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class CucumberMainTest {
    public static void main(String[] args) throws IOException {
        ClassLoader classLoader = CucumberMainTest.class.getClassLoader();
        ResourceLoader resourceLoader = new MultiLoader(classLoader);
        ClassFinder classFinder = new 
        ResourceLoaderClassFinder(resourceLoader, classLoader);
        List<String> pluginList = new ArrayList<>();
        pluginList.add("--plugin");
        pluginList.add("html:target/cucumber-html-report");
        pluginList.add("--plugin");
        pluginList.add("json:target/cucumber.json");
        RuntimeOptions ro = new RuntimeOptions(pluginList);
        ro.getFilters().add("@passed");
        ro.getFeaturePaths().add("src/test/resources");
        ro.getGlue().add("cucumberTest/steps");
        cucumber.runtime.Runtime runtime = new 
        cucumber.runtime.Runtime(resourceLoader, classFinder, classLoader,
            ro);
        runtime.run();

    }
}

It executes my test cases but does not load my SpringContext, as a result no spring beans are loadedand i get null pointer exception.. Any help is geatly appreciated.

Regards, Vikram Pathania

1

There are 1 best solutions below

0
On

One solution i found was to run RunGwMLCompareTests class from outside which handles my Spring context automatically. So basically i am using gradle to create batch file which does nothing but automatically creates a batch with all relative depencies in my class path and then run following command:

   "%JAVA_EXE%" -classpath "%CLASSPATH%" org.junit.runner.JUnitCore  
   cucumberTest.runners.RunGwMLCompareTests

where,

     JAVA_EXE=C:\Program Files\Java\jdk1.7.0_65/bin/java.exe

And CLASSPATH is lib folder where all jars are present.

    CLASSPATH=%APP_HOME%\lib\*.jar

Now running this batch i am using

features = { "src/test/resources" } <tag> 

to put my feature files outside so that it can edited on the fly. I hope this helps somebody out there.

by doing this , i am able to run RunGwMLCompareTests class. Now using this config my Cucumber test cases are running and It also loads my Spring boot context and then exceutes all cases defined in feature.

Regards, Vikram Pathania