猿问

如何从交互中返回值

我有一个从 postgres 数据库中进行选择的剧本交互类。我想知道如何使用这个剧本模式返回结果集。


我现在尝试的是将方法“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();

    }

}

现在我能够执行选择操作但不能检索结果集。


我希望能够检索它或让它返回到使用此交互的任何任务或问题


翻过高山走不出你
浏览 143回答 1
1回答

杨__羊羊

在剧本模式中,交互并不意味着返回值,只是为了在被测系统上执行一些操作。您使用问题来查询系统的状态。如果您分享您尝试解决的整体问题会更容易,但由于您的示例是关于查询用户的,您可以进行测试来检查您是否可以通过 UI 管理页面将用户添加到系统:@Testpublic void add_a_user_to_the_system() {&nbsp; &nbsp; Actor ada = Actor.named("Ada").describedAs("an admin");&nbsp; &nbsp; when(ada).attemptsTo(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddANewUser.called("Jack")&nbsp; &nbsp; );&nbsp; &nbsp; then(ada).should(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seeThat(KnownUsers.inTheSystem(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contains(hasProperty("name", equalTo(("Jack"))))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );}(在这种情况下,按名称搜索用户可能会更有效,但我想让它接近您的示例)。为此,您可能有一个AddANewUser使用管理屏幕添加新用户的类:class AddANewUser implements Performable {&nbsp; &nbsp; public static Performable called(String userName) {&nbsp; &nbsp; &nbsp; &nbsp; return instrumented(AddANewUser.class, userName);&nbsp; &nbsp; }&nbsp; &nbsp; private final String userName;&nbsp; &nbsp; AddANewUser(String userName) {&nbsp; &nbsp; &nbsp; &nbsp; this.userName = userName;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public <T extends Actor> void performAs(T actor) {&nbsp; &nbsp; &nbsp; &nbsp; // Add a new user called userName via the UI&nbsp; &nbsp; }}然后你会使用一个问题来检查这个用户是否存在:@Subject("known users")static class KnownUsers implements Question<List<ApplicationUser>> {&nbsp; public static KnownUsers inTheSystem() { return new KnownUsers(); }&nbsp; @Override&nbsp; public List<ApplicationUser> answeredBy(Actor actor) {&nbsp; &nbsp; &nbsp; // Query the database and convert the result set to ApplicationUsers&nbsp; &nbsp; &nbsp; return ...;&nbsp; }}您还可以创建一个 Ability 类来集中 JDBC 查询、凭据等。
随时随地看视频慕课网APP

相关分类

Java
我要回答