java - org.jbehave.core.steps.Steps$DuplicateCandidateFound : THEN the result will be $expected

标签 java jbehave

我有以下 jbehave 故事文件:

Scenario: Scene1
Given the number of <input>
When the result is estimated
Then the result will be <expected>

Examples:
|input|expected|
|1|1|
|2|1, 2|
|3|1, 2, 3|
|4|1, 2, 3, 4|

Scenario: Scene2
Given the number of <input>
When the result is estimated
And the result is sorted in descending order
Then the result will be <expected>

Examples:
|input|expected|
|1|1|
|2|2, 1|
|3|3, 2, 1|
|4|4, 3, 2, 1|

现在我想在我的程序中测试这两种情况,所以我编写了以下代码:

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

public class EstimatorSteps {
    private Estimator estimator;

    @Given("the number of $input")
    public void given(int input) {
        estimator = new Estimator(input);
    }

    @When("the result is estimated")
    public void when1() {
        estimator.estimate(estimator.getInput());
    }

    @Then("the result will be $expected)
    public void then1(List<Integer> result) {
        assertEquals(estimator.getResult(), result);
    }

    @When("the result is sorted in descending order")
    public void when2() {
        estimator.descending(estimator.getResult());
    }

    @Then("the result will be $expected)
    public void then1(List<Integer> result) {
        assertEquals(estimator.getResult(), result);
    }
}

当我运行测试用例时,我收到以下错误消息:

org.jbehave.core.steps.Steps$DuplicateCandidateFound: THEN the result will be $expected

测试这两种情况的正确方法是什么,我必须在 Java 代码中进行哪些更改。我不想更改我的故事文件。

这是我的 JBehave 配置文件:

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;

import java.util.Arrays;
import java.util.List;

import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
import static org.jbehave.core.reporters.StoryReporterBuilder.Format.CONSOLE;

public class JBehaveStories extends JUnitStories {

    @Override
    public Configuration configuration() {
        return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(this.getClass()))
                .useStoryReporterBuilder(new StoryReporterBuilder()
                        .withCodeLocation(codeLocationFromClass(this.getClass())).withFormats(CONSOLE));
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(), new EstimatorSteps());
    }

    @Override
    protected List<String> storyPaths() {
        return new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()),
                Arrays.asList("**/*.story"), Arrays.asList(""));
    }
}

最佳答案

EstimatorSteps中有两个相同的@Then步骤类:

@Then("the result will be $expected)
public void then1(List<Integer> result) {
    assertEquals(estimator.getResult(), result);
}

....

@Then("the result will be $expected)
public void then1(List<Integer> result) {
    assertEquals(estimator.getResult(), result);
}

JBehave 提示:

DuplicateCandidateFound: THEN the result will be $expected
<小时/>

删除其中一个方法,错误就不会出现。

顺便说一句,我想知道这个类是如何编译的,因为 Java 不应该允许两个具有完全相同签名的重载方法。

关于java - org.jbehave.core.steps.Steps$DuplicateCandidateFound : THEN the result will be $expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46174268/

相关文章:

java - 使用 ObjectMapper 反序列化到参数化类时,甚至使用 TypeReference 和 TypeFactory 时出现 ClassCastException

java - 为什么我必须使用 for 循环而不是 foreach 循环?

java - JBehave 面临的问题

java - 发生异常时 JBehave 不会导致构建失败

java - JBehave - 运行单个场景

java - 我无法在 android studio 中创建项目。显示插件错误。许多插件包括java gradle也无法加载

JPanel 中的 java swing 快捷键/键绑定(bind)

java - 用于测试 REST Web 应用程序的 JBehave

java - 如何在 jbehave 中的后续场景中获取当前故事/场景

java - cpplex java多维决策变量,最小化