代码说明 - Java新手入门 - 测试自动化框架

时间:2015-12-22 00:17:11

标签: java

我是Java新手并尝试开发测试自动化框架。我一直在关注教程。我正在努力理解一些代码。如果有人可以逐步解释代码是如何工作或任何引用的话,那真的很棒。谢谢你提前!

@Test 

   NewPostPage.CreatePost("This is the test post title").WithBody("Hi, this is the body").publish();
public class NewPostPage {

public static CreatePostCommand CreatePost(String title) {
return new CreatePostCommand(title);

}
public class CreatePostCommand {

private final String title;
private String body;

public CreatePostCommand(String title){
    this.title=title;

}

public CreatePostCommand WithBody(String body){

    this.body=body;
    return this;

}

1 个答案:

答案 0 :(得分:2)

我将解释代码,因为这就是你的要求。

这是一个静态方法,这意味着它是一个类级方法,而不是实例级方法。您可以使用classname后跟方法名称来调用它。

NewPostPage.CreatePost() 

定义
public static CreatePostCommand CreatePost(String title)

它返回带有适当参数的CreatePostCommand对象。

return new CreatePostCommand(title);

CreatePostCommand的构造函数接受一个String。

public CreatePostCommand(String title){
    this.title=title;
}

然后,WithBody方法有一个Builder模式。 Builder模式的目的是在一行上将多个WithX调用链接在一起,而不是为setXsetY方法使用多行。每个WithX调用都会返回它正在构建的对象。

return this;

现在,我猜你遗漏了publish方法?但是你所拥有的就等同于此。

CreatePostCommand postCmd = NewPostPage.CreatePost("This is the test post title");
postCmd = postCmd.WithBody("Hi, this is the body");
postCmd.publish();
相关问题