jBehave步骤方法的NullPointerException

时间:2014-10-10 08:00:35

标签: selenium nullpointerexception selenium-webdriver jbehave

我有以下jBehave故事/场景;

Scenario: some info validation
When I enter the population as <global_target_pop> and submit
Then I should see validation message <message> for <field>

Examples:
| field | global_target_pop | message |
| targetPopulation | a1 | Population should be an Integer |

相应的步骤方法如下;

@Then("Then I should see validation message <message> for <field>")
    public void checkValidationMessageForField(String message, @Named("value") String reason, @Named("field") String fieldName) {

    }

但是,我正在为此特定步骤获取NullPointerException。下面是堆栈跟踪;

java.lang.NullPointerException
    at org.jbehave.core.steps.StepCreator.parameterPosition(StepCreator.java:404)
    at org.jbehave.core.steps.StepCreator.parameterForPosition(StepCreator.java:310)
    at org.jbehave.core.steps.StepCreator.parameterValuesForStep(StepCreator.java:296)
    at org.jbehave.core.steps.StepCreator.access$1000(StepCreator.java:36)
    at org.jbehave.core.steps.StepCreator$ParametrisedStep.parametriseStep(StepCreator.java:639)
    at org.jbehave.core.steps.StepCreator$ParametrisedStep.perform(StepCreator.java:592)
    at org.jbehave.core.embedder.StoryRunner$FineSoFar.run(StoryRunner.java:535)

问题是什么?

1 个答案:

答案 0 :(得分:0)

为方法声明中的所有参数指定显式名称:

 public void checkValidationMessageForField(
     @Names("message") String message, 
     @Named("value") String reason, 
     @Named("field") String fieldName) {
 }

如果使用表格示例来对场景进行参数化,即使用&#39;&lt;名称&gt;&#39;在步骤中的语法,然后匹配的java方法中的所有参数必须使用命名参数(带@Named注释的参数):

http://jbehave.org/reference/latest/parametrised-scenarios.html

  

使用表格示例时强调的一个重要区别是   它们需要命名参数才能匹配的候选步骤   Java方法。命名参数允许注入参数   而是使用表行值和相应的标题名称   从注释模式匹配中提取。因此,   步骤注释模式必须包含逐字文本步骤,例如:

       @Given("a stock of <symbol> and a <threshold>") public void
       aStock(@Named("symbol") String symbol, @Named("threshold") double
       threshold) {
                   // ... }

&#34;值&#34;也存在问题。参数 - 使用@Named("value")注释在java方法中声明,但不会出现在场景中。
您需要在示例表中为此参数指定值,或者在场景中使用元标记:

Examples:
| field            | global_target_pop | message       | value      |
| targetPopulation | a1                | Population... | some value |

或:

Meta:
@value some value

Scenario: some info validation
When I enter the population as <global_target_pop> and submit
Then I should see validation message <message> for <field>

Examples:
| field | global_target_pop | message |
| targetPopulation | a1 | Population should be an Integer |
相关问题