2015-11-19 76 views
0

我只是使用JBehave的BDD开始,我有一个很大的问题。 每次断言失败时,我的Netbeans环境中都会抛出一个异常,而其他测试不会执行。 但我希望它将此测试标记为失败并执行其他测试。 我搜索了很多,但没有解决我的问题。JBehave:不要把断言异常

故事:

Narrative: 
 
In order to manage our stock efficiently 
 
As a logistic employee 
 
I would like to handle returns and changes in a proper way 
 
    
 
    
 
Scenario: Return of a phone 
 
    
 
Given there is an empty stock 
 
And there are 2 A phone(s) in stock 
 
When a customer returns the not damaged A phone 
 
Then there should be 2 A phone(s) in stock 
 

 
Scenario: Exchange of a phone 
 
    
 
Given there is an empty stock 
 
And there are 5 A phone(s) in stock 
 
And there are 5 B phone(s) in stock 
 
When a customer changes his A phone for a B phone 
 
Then there should be 6 A phone(s) in stock 
 
And there should be 4 B phone(s) in stock

ScenarioLoader

public class LogisticScenarios extends JUnitStory { 

@Override 
public Configuration configuration() { 
    URL storyURL = null; 
    try { 
     // This requires you to start Maven from the project directory 
     storyURL = new URL("file://" + System.getProperty("user.dir") 
       + "/src/main/resources/stories/"); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    return new MostUsefulConfiguration() 
      .usePendingStepStrategy(new FailingUponPendingStep()) 
      .useStoryLoader(
      new LoadFromRelativeFile(storyURL)).useStoryReporterBuilder(
        new StoryReporterBuilder().withFormats(Format.HTML)); 
} 

@Override 
public List<CandidateSteps> candidateSteps() { 
    return new InstanceStepsFactory(configuration(), new LogisticSteps()) 
      .createCandidateSteps(); 
} 

@Override 
@Test 
public void run() { 
    try { 
     super.run(); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
} 

}

步骤

public class LogisticSteps extends Embedder { 

List<Phone> stock; 

@Given("there is an empty stock") 
public void initializeStock() { 
    stock = new LinkedList(); 
} 

@Given("there are $quantity $type phone(s) in stock") 
public void fillStock(String quantity, String type) { 
    stock = stock == null ? new LinkedList() : stock; 
    PhoneType pType = type.equals("A") ? PhoneType.A : PhoneType.B; 

    addPhonesToStock(pType, Integer.parseInt(quantity)); 
} 

@When("a customer returns the not damaged $type phone") 
public void customerReturnsPhone(String type) { 
    PhoneType ptype = type.equals("A") ? PhoneType.A : PhoneType.B; 
    addPhonesToStock(ptype, 1); 
} 

@When("a customer changes his $type1 phone for a $type2 phone") 
public void customerExchangesPhone(String type1, String type2) { 
    PhoneType pType1 = type1.equals("A") ? PhoneType.A : PhoneType.B; 
    PhoneType pType2 = type2.equals("A") ? PhoneType.A : PhoneType.B; 

    stock.add(new Phone(pType1)); 
    removePhoneFromStock(pType2); 
} 

@Then("there should be $quantity $type phone(s) in stock") 
public void thePositionReturnedShouldBe(String quantity, String type) { 
    PhoneType pType = type.equals("A") ? PhoneType.A : PhoneType.B; 

    Assert.assertEquals(Integer.parseInt(quantity), countPhonesFromType(pType)); 
} 

private void removePhoneFromStock(PhoneType type) { 
    for (int i = 0; i < stock.size() - 1; i++) { 
     if (stock.get(i).getType() == type) { 
      stock.remove(i); 
      break; 
     } 
    } 
} 

private int countPhonesFromType(PhoneType type) { 
    int count = 0; 
    for (Phone p : stock) { 
     if (p.getType() == type) { 
      count++; 
     } 
    } 
    return count; 
} 

private void addPhonesToStock(PhoneType type, int quantity) { 
    for (int i = 0; i < quantity; i++) { 
     stock.add(new Phone(type)); 
    } 
} 

}

感谢您的帮助

回答

0

JBehave的一般行为。在一个情况下,如果一个步骤失败(抛出异常)不执行该方案的下一步(它们标记为未执行),但在那之后第二个方案将被执行。现在,如果你的下一个场景取决于第一场景的结果,那么它也将失败,对。

现在,如果你看你有:

try { 
     super.run(); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 

要在来到这个设置,可以尝试捕捉异常的代码,并采取必要的步骤,按你的选择。

放置在您正在做的断言使用尝试捕捉这样的:

try{ 
    Assert.assertEquals(); 
} catch (Exception e){ 
    <your steps> <record the result or just move forward> 
}