如何从交互中返回值

时间:2019-02-02 16:32:50

标签: java serenity-bdd cucumber-serenity

我有一个剧本互动类,可以从postgres DB中进行选择。我想知道如何使用返回这个剧本模式的结果集。

我现在尝试的是将方法“ public void performAs(T actor)”的返回类型更改为“ public ResultSet performAs(T actor)”

但是,我具有以下问题:

Error:(12, 8) java: jarv.serenity.carnival.interactions.dbRelated.SelectDataBase is not abstract and does not override abstract method <T>performAs(T) in net.serenitybdd.screenplay.Performable
Error:(23, 40) java: <T>performAs(T) in jarv.serenity.carnival.interactions.dbRelated.SelectDataBase cannot implement <T>performAs(T) in net.serenitybdd.screenplay.Performable
  return type java.sql.ResultSet is not compatible with void
Error:(21, 5) java: method does not override or implement a method from a supertype

这是课程文件

package jarv.serenity.carnival.interactions.dbRelated;

import jarv.serenity.carnival.dataBaseConection.DataBaseDriver;
import jarv.serenity.carnival.model.Users;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Interaction;
import net.thucydides.core.annotations.Step;
import java.sql.ResultSet;
import java.sql.Statement;
import static net.serenitybdd.screenplay.Tasks.instrumented;

public class SelectDataBase implements Interaction {

    private final DataBaseDriver dbDriver;
    private ResultSet rs=null;

    public SelectDataBase(DataBaseDriver dbDriver) {
        this.dbDriver = dbDriver;
    }

    @Override
    @Step("Selecting from DB")
    public <T extends Actor> void performAs(T actor) {
        try
        {
            Statement st = dbDriver.getConn().createStatement();
            rs = st.executeQuery("SELECT * From users");
            dbDriver.disconect();
        }
        catch(Exception e)
        {
            dbDriver.disconect();
            System.out.println(e);
        }
    }

    public ResultSet getResults(){
        return rs;
    }

    public static Interaction select(DataBaseDriver dbDriver) {
        return instrumented(SelectDataBase.class, dbDriver);
    }

    @Override
    public String toString() {
        return rs.toString();
    }
}

现在,我能够进行选择操作,但没有检索结果集。

我想能够检索它还是有它返回的任何任务或使用该互动问题

1 个答案:

答案 0 :(得分:0)

在Screenplay模式中,交互并不意味着返回值,而只是为了在被测系统上执行某些操作。您可以使用Questions查询系统状态。如果您共享要解决的总体问题会更容易,但是由于您的示例是关于查询用户的,因此您可以进行一项测试,以检查是否可以通过UI管理员页面将用户添加到系统中。

@Test
public void add_a_user_to_the_system() {

    Actor ada = Actor.named("Ada").describedAs("an admin");

    when(ada).attemptsTo(
            AddANewUser.called("Jack")
    );

    then(ada).should(
            seeThat(KnownUsers.inTheSystem(),
                    contains(hasProperty("name", equalTo(("Jack"))))
            )
    );
}

(在这种情况下,按名称搜索用户可能会更有效,但我想使其与您的示例保持一致)。

为此,您可能有一个AddANewUser类,该类使用管理屏幕来添加新用户:

class AddANewUser implements Performable {

    public static Performable called(String userName) {
        return instrumented(AddANewUser.class, userName);
    }

    private final String userName;

    AddANewUser(String userName) {
        this.userName = userName;
    }

    @Override
    public <T extends Actor> void performAs(T actor) {
        // Add a new user called userName via the UI
    }
}

然后,您将使用以下问题检查该用户的存在:

@Subject("known users")
static class KnownUsers implements Question<List<ApplicationUser>> {

  public static KnownUsers inTheSystem() { return new KnownUsers(); }

  @Override
  public List<ApplicationUser> answeredBy(Actor actor) {
      // Query the database and convert the result set to ApplicationUsers
      return ...;
  }
}

您还可以创建一个Ability类来集中化JDBC查询,凭据等。