简体   繁体   English

在Main类中运行Cucumber和Spring引导

[英]Running Cucumber and Spring boot in Main class

My requirement is to run Cucumber test cases using Spring boot to run through a Custom java main class. 我的要求是使用Spring Boot通过Custom java主类运行Cucumber测试用例。 I am able to run Cucumber test suite fine if i am using following config class:- 如果我使用以下配置类,则可以正常运行Cucumber测试套件:-

@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. 当我运行RunGwMLCompareTests类时,现在使用此配置,我的Cucumber测试用例正在运行,它将加载Spring引导上下文,然后执行feature中定义的所有用例。

Now my issue is that i want to run this from seperate main java class. 现在,我的问题是我想从单独的主要Java类运行它。 I have created my java class as follows :- 我创建了我的java类,如下所示:

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. 它执行我的测试用例,但不加载我的SpringContext,因此没有加载Spring Bean,并且我得到了空指针异常。

Regards, Vikram Pathania 问候,维克拉姆Pathania

One solution i found was to run RunGwMLCompareTests class from outside which handles my Spring context automatically. 我发现的一种解决方案是从外部运行RunGwMLCompareTests类,该类自动处理我的Spring上下文。 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: 因此,基本上我正在使用gradle创建批处理文件,该批处理文件什么也不做,但是会自动在我的类路径中创建具有所有相对依赖关系的批处理,然后运行以下命令:

   "%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是所有jar都存在的lib文件夹。

    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. 通过这样做,我能够运行RunGwMLCompareTests类。 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. 现在,使用此配置,我的Cucumber测试用例正在运行,并且还将加载Spring引导上下文,然后执行feature中定义的所有用例。

Regards, Vikram Pathania 问候,维克拉姆Pathania

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM