尝试实现cucumber来做一些自动化测试。 j单元测试。我创建了 2 个文件并编辑了 Maven 项目附带的 pom.xml 以添加依赖项。内容如下所示。两个文件中的第一个是 cucumber .feature 文件,它是简单语言的小黄瓜。另一个是CukesRunner.java
当我使用
Project -> Run as ... -> Maven test
运行测试时,它按预期工作。
但是,当我使用 Eclipse Eclipse JUnit GUI 运行 CukesRunner.java 文件时,出现错误:
java.lang.NoSuchMethodError: org.junit.runner.Description.createSuiteDescription(Ljava/lang/String;Ljava/io/Serializable;[Ljava/lang/annotation/Annotation;)Lorg/junit/runner/Description;
at cucumber.runtime.junit.FeatureRunner.getDescription(FeatureRunner.java:43)
at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:77)
at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:41)
at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:226)
...
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bdd</groupId>
<artifactId>airportparking</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>airportparking</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rubiconproject.oss</groupId>
<artifactId>jchronic</artifactId>
<version>0.2.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
CukesRunner.java:
package com.bdd.airportparking;
import cucumber.api.junit.*;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Cucumber.Options(
format={"pretty", "html:target/cucumber"},
features="src/test/resources"
)
public class CukesRunner {
}
代客泊车功能:
Feature: Valet Parking
As a traveler
In order to determine where to park my car
I want to know the cost of valet parking
Scenario: Calculate valet parking cost for half an hour
When I park my car in the Valet Parking Lot for 30 minutes
Then I will have to pay $12
将
CukesRunner.java
作为 Junit Test
运行时的输出:
java.lang.NoSuchMethodError: org.junit.runner.Description.createSuiteDescription(Ljava/lang/String;Ljava/io/Serializable;[Ljava/lang/annotation/Annotation;)Lorg/junit/runner/Description;
at cucumber.runtime.junit.FeatureRunner.getDescription(FeatureRunner.java:43)
at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:77)
at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:41)
at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:226)
at org.junit.runner.Runner.testCount(Runner.java:38)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.countTestCases(JUnit4TestClassReference.java:30)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.countTests(RemoteTestRunner.java:487)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:455)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我如何在 Eclipse 中构建我的项目:
http://postimg.org/image/vf6tlw7el/full/
更新你的 junit 版本以及你的 Surefire 插件也许可以解决这个问题。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
万无一失:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
</plugins>
</build>
我遇到了同样的问题,并在配置最新版本后得到修复。该问题出现在 junit 版本 4.10 上。 4.11 的最新版本效果很好
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
不同的故事,但相同的问题...使用 gradle 开发 Jenkins 插件,在我的类路径上有最新的
junit:junit:4.12
库...
该问题是由
junit:junit-dep:4.10
库(“又名”)引起的...
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
将其从我的配置类路径中明确删除后,我不再遇到这个问题。
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.mkolisnyk</groupId>
<artifactId>cucumber-reports</artifactId>
<version>0.0.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Werror</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
我遇到了同样的问题,我发现我在构建路径中配置了 junit 4.10 和 4.11,遵循 junit 11,解决了问题。
遇到同样的问题,并通过使用 JUnit 版本 4.11 或更高版本得到解决。参考:https://groups.google.com/forum/#!topic/cukes/et3rd_0LVRU
确保您在 POM.xml 中使用正确的 JUnit 版本。 将其从 4.10 更改为最新版本 4.11 就可以了。
您看到的错误是由于 Cucumber 期望的 JUnit 版本与类路径上实际的版本不匹配造成的。 也可能是使用旧功能的某些依赖性导致了不匹配。更多内容请参见下文。
方法
org.junit.runner.Description.createSuiteDescription(String, Serializable, Annotation[])
是在 JUnit 4.12 中添加的,但您使用的是 JUnit 4.8.2。
尝试将 pom.xml 文件中的 JUnit 版本更新为 4.12 或更高版本。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
更改,在 Eclipse 中重新导入 Maven 依赖项(右键单击项目 -> Maven -> 更新项目),然后再次尝试运行测试。这应该有助于解决特定的一般问题
NoSuchMethodError
我必须解决的是依赖冲突。我正在使用定制的测试自动化框架
afor.co.nz
。我在 bitbucket 管道中运行该框架。我的 pom.xml
中有 15 个依赖项。我不得不一一取消注释,直到找到导致问题的库。 2012 年的依赖 json-simple 1.1.1
[INFO] Surefire report directory: /opt/atlassian/pipelines/agent/build/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running RunTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.547 sec <<< FAILURE!
RunTest Time elapsed: 0.546 sec <<< ERROR!
java.lang.NoSuchMethodError: 'org.junit.runner.Description org.junit.runner.Description.createSuiteDescription(java.lang.String, java.io.Serializable, java.lang.annotation.Annotation[])'
at io.cucumber.junit.FeatureRunner.getDescription(FeatureRunner.java:118)
at io.cucumber.junit.Cucumber.describeChild(Cucumber.java:191)
at io.cucumber.junit.Cucumber.describeChild(Cucumber.java:89)
at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:290)
at org.junit.runners.ParentRunner.run(ParentRunner.java:296)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Results :
Tests in error:
RunTest: 'org.junit.runner.Description org.junit.runner.Description.createSuiteDescription(java.lang.String, java.io.Serializable, java.lang.annotation.Annotation[])'
[INFO] Surefire report directory: /opt/atlassian/pipelines/agent/build/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running RunTest
SLF4J(I): Connected with provider of type [org.apache.logging.slf4j.SLF4JServiceProvider]
Logging host stats to the preferred location
@loginO9
Scenario: login with valid credentials # src/test/resources/features/web/O9Login.feature:5
Starting scenario: login with valid credentials
我已将 junit 驱动程序从 4.9 更新到 4.11;这绝对是junit驱动的问题,所以只要更新它就可以了。