2017-09-12 27 views
1

我有以下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 $ DuplicateCandi发现日期:那么结果 将是$有望

什么是测试两种情况下正确的方式,什么样的变化我在我的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("")); 
    } 
} 

回答

2

有在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不应该允许两个重载的方法具有完全相同的签名。

+0

这两种方法都缺少结束引号。我也很好奇这是如何编译的。 –

相关问题