2015-11-19 32 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> 
}