Fitnesse - 将异常映射到失败

时间:2018-02-05 15:08:22

标签: fitnesse

情境:如果一切正常,我们的客户端设计为只运行(void)并抛出专用异常,如果发生错误情况(例如,使用已存在的用户名注册用户)。

我们正在使用Slim Decision Tables。示例夹具如下所示:

public class MyFixture {

    private final Client client;
    private String someKey;
    private String accessToken;

    public MyFixture() {
        client = initClient();
    }

    public void setSomeKey(final String someKey) {
        this.someKey = someKey;
    }

    public void setAccessToken(final String accessToken) {
        this.accessToken = accessToken;
    }

    public final void execute() {
        // this execution may throw an Exception that inherits from RuntimeException 
        // and contains a String field called errorCode which can validate against
        client.executeFailableRequest(someKey, accessToken);
    }
}

问题:Fitnesse会显示黄色异常,但我希望将其视为红色故障。这可能吗?

我们知道捕获异常并评估其内容是可能的,但更喜欢不那么庞大的方式。

1 个答案:

答案 0 :(得分:0)

我不相信你可以在决策表的execute()方法中使用红色。但是你可以在那里捕获异常并在表中明确地评估是否抛出了错误。类似的东西:

public class MyFixture {

    private final Client client;
    private String someKey;
    private String accessToken;
    private String lastError;

    public MyFixture() {
        client = new Client();
    }

    public void setSomeKey(final String someKey) {
        this.someKey = someKey;
    }

    public void setAccessToken(final String accessToken) {
        this.accessToken = accessToken;
    }

    public final void execute() {
        try {
            // this execution may throw an Exception that inherits from RuntimeException
            // and contains a String field called errorCode which can validate against
            client.executeFailableRequest(someKey, accessToken);
            lastError = null;
        } catch (MyError e) {
            lastError = e.getErrorCode();
        }
    }

    public String errorCode() {
        return lastError;
    }
}

和表

|my fixture                      |
|some key|access token|errorCode?|
|key     |a           |null      |
|a       |b           |null      |

如果许多方法都可以抛出错误/异常(即你不能只在execute方法中添加try / catch但在许多方法中必须这样做),你可以使你的夹具实现InteractionAwareFixture。这允许您“拦截”从Slim到夹具的所有调用,并添加自定义处理(如应用于所有调用的方面)。

您还可以将您的灯具重写为'table table',从而更好/完全控制您的表格评估方式。然后,您可以选择要为哪些单元格提供哪种颜色。

最后一种方法是实现自己的Slim表,而不是使用决策表,但这在我的经验中可能非常复杂,并且对于手头的问题似乎有点过头了。