2017-07-15 108 views
0

我有一個maven項目運行cucumberselenium。在src/features中定義了*.feature文件,並且在src/test/java/com/example/steps中定義了這些步驟。黃瓜:找不到類com.example.runner.RunnerTest

enter image description here

我還定義如下的測試轉輪類:

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"並與抱怨:

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 -> Cucumber

回答

0

我會考慮重新組織以下列方式您的結構:

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

這將改善您的代碼組織和可追溯性,以防測試失敗。然後

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。您現在應該可以運行CheckboxTest