我下面有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:然后结果
  将是预期的


测试这两种情况的正确方法是什么,我必须在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:然后结果将是$ expected,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46174268/

10-09 08:00