首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

JBehave学习札记(3)-高级特性介绍

2012-07-29 
JBehave学习笔记(3)--高级特性介绍1、Composite Steps顾名思义就是能够把多个Steps组合为一个Step。例子Meta

JBehave学习笔记(3)--高级特性介绍
1、Composite Steps
顾名思义就是能够把多个Steps组合为一个Step。例子

Meta:@author liuxianning@theme  compositeNarrative: This story is used to show composite steps.Scenario: Add a student into the classGiven There is a student with default detailsWhen system add the student into classThen we can get student 'Lincoln2' from class


然后在StudentSteps中增加方法
    @Given("There is a student with default details")    @Composite(steps = {            "Given his name is 'Lincoln2'",            "Given his age is '18'",            "Given his hobby is 'basketball'",            "Given his father's name is 'Mike'",            "Given his mother's name is 'Mary'",            "Given his brother's name is 'Luis'"    })    public void createStudentWithDefaultDetails() {    }

这样就可以用一句话执行之前已经实现的很多步骤。

2、Tabular parameters
表结构参数,这是一个简化和结构化参数输入的特性,我们可以在一个步骤中把需要传递的多个参数结构化的传递给Step方法。例子:
Meta:@author liuxianning@theme  tableNarrative: In order to get a new student,as a teacher, I want to add a student into the ClassScenario: Add a student into the classGiven There is a student  with details:|name    |age |hobby      |father name |mother name |brother name||lincoln3|18  |basketball |Mike        |Mary        |Luis        |When system add the student into classThen we can get student 'lincoln3' from class

可以通过一个Table以键值对的形势来输入参数,在StudentSteps添加对应的方法来接收参数
@Given("There is a student  with details:$details")    public void setDetails(@Named("details") ExamplesTable details) {        student = new Student();        Parameters parameters = details.getRowAsParameters(0);        student.setName(parameters.valueAs("name",String.class));        student.setAge(parameters.valueAs("age",Integer.class));        student.setHobby(parameters.valueAs("hobby",String.class));        student.setFatherName(parameters.valueAs("father name",String.class));        student.setMotherName(parameters.valueAs("mother name",String.class));        student.setBrotherName(parameters.valueAs("brother name",String.class));    }


3、Examples
Scenario多实例,这个特性主要的目的在于可以使用不同的数据反复执行一个Scenario。例子:
Meta:@author liuxianning@theme  examplesNarrative: In order to get a new student,as a teacher, I want to add a student into the ClassScenario: Add a student into the classGiven There is a studentAnd his name is '$name'And his age is '$age'When system add the student into classThen we can get student '$studentName' from classExamples:|name|age|studentName||lxn1|18 |lxn1       ||lxn2|19 |lxn2       ||lxn3|20 |lxn3       |

该实例中回使用Examples中的数据反复执行该Scenario。

4、Meta Filtering
Meta信息过滤,这个特性允许JBehave指定运行特定的Story或者Scenario。使用方法为在配置Embedder的时候添加上MetaFilter属性。
public Embedder configuredEmbedder() {        Embedder embedder = new Embedder();        embedder.useConfiguration(configuration());        embedder.useCandidateSteps(candidateSteps());        embedder.useStepsFactory(stepsFactory());      [color=red]  embedder.useMetaFilters(asList("+theme composite", "-skip"));[/color]        return embedder;    }

在标红的代码段中指明了只执行Meta段中属性theme为composite的Story。

5、GivenStories
Story依赖,这个特性让一个Story可以作为另一个Story的Scenario的前提条件。例子
Meta:@author liuxianning@theme  givenstoriesNarrative: In order to maintian student's info,as a teacher, I want to update a student's nameScenario: Add a student into the classGivenStories: AddStudentIntoClass.storyGiven Get student "Lincoln"When system Update student name to "Lin"Then we can get student 'Lin' from class

热点排行