黄瓜:找不到类 com.example.runner.RunnerTest

2022-01-23 00:00:00 cucumber java maven cucumber-jvm

我有一个 maven 项目来运行 cucumberselenium.*.feature文件定义在src/features,步骤定义在src/test/java/com/example/steps.

I have a maven project to run cucumber and selenium. The *.feature files are defined in the src/features, and the steps are defined in the src/test/java/com/example/steps.

我还定义了一个测试运行器类,如下所示:

I have also defined a test runner class as below:

package com.example.runner;

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

@RunWith(Cucumber.class)
@CucumberOptions(
             features={"src/features"},
             glue="com/example/steps")
public class RunnerTest {

}

问题是:当我运行它抱怨的课程时:

Class not found com.example.runner.RunnerTest
java.lang.ClassNotFoundException: com.example.runner.RunnerTest
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:688)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

问题是,为什么不识别跑步者的名字?

更新:

当我运行功能文件时

打开特征文件->右键->运行为->1个黄瓜特征

效果不错

我将 glue="com/example/steps" 更改为 glue="com.example.steps" 并抱怨:

I changed the glue="com/example/steps" to glue="com.example.steps" and it complains with :

java.lang.NullPointerException
    at com.example.steps.Click_Steps.i_am_in(Click_Steps.java:23)
    at ✽.Given I am in "http://suvian.in/selenium/1.6checkbox.html"(CheckBox.feature:14)

当我参考文件时:

base.getDriver().navigate().to(uri);

但正如我所说,当我使用 Run as -> 运行功能文件时,这条线工作得很好.黄瓜

But as i said, this line work pretty well when i run the feature file using Run as -> Cucumber

推荐答案

我会考虑通过以下方式重新组织您的结构:

I would consider re-organizing your structure in the following way:

src/
├── main/
│   └── java/
|        └── ...
│   
└── test/
    ├── java/
    |    └── com.example/
    |         ├── checkbox/
    |         |    ├── CheckboxSteps/
    |         |    └── CheckboxTest/
    |         ├── click_button/
    |         |    ├── ClickButtonSteps/
    |         |    └── ClickButtonTest/
    |         └── ...
    |
    └── resources/
         └── features/
              ├── checkbox/
              |    └── checkbox.feature/
              ├── click_button/
              |    └── ClickButton.feature/
              └── ...

这将在测试失败的情况下改善您的代码组织和可追溯性.

This will improve both your code organization and traceability in case of failing tests.

CheckboxTest 应如下所示:

package com.example.checkbox;

   import cucumber.api.CucumberOptions;
   import cucumber.api.SnippetType;
   import cucumber.api.junit.Cucumber;
   import org.junit.runner.RunWith;

   @RunWith(Cucumber.class)
   @CucumberOptions(
                snippets = SnippetType.CAMELCASE,
                glue = {"com.example.checkbox"},
                features = "classpath:features/checkbox")
   public class CheckboxTest {

    /*
    * This class should be empty, step definitions should be in separate classes.
    */

   }

然后将您的步骤定义放入 CheckboxSteps.

Then put your step definitions in CheckboxSteps.

你现在应该可以运行CheckboxTest了.

You should be able to run CheckboxTest now.

相关文章