Cucumber JVM:测试是否抛出了正确的异常 [英] Cucumber JVM : Test if the correct exception is thrown

查看:275
本文介绍了Cucumber JVM:测试是否抛出了正确的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点惊讶我在网上找不到这个问题的答案。
无论如何,当使用Junit来测试抛出正确的异常时,我会做这样的事情:

I am a bit surprised that I cannot find an answer to this question online. Anyways when using Junit, to test that a correct exception is thrown, I would do something like this:

@Test(expected = NullPointerException.class)
    public void testExceptionThrown(){

        taskCreater.createTask(null);
    }

正如您所见,这是一种非常优雅的测试方法异常被抛出,但是当我使用黄瓜JVM时,我怎么能达到同样的优雅呢?我的测试现在看起来像这样:

As you can see above, it is a very elegant way of testing if an exception is thrown, But how can I achieve the same elegance, when using cucumber JVM ? My test looks like this right now:

@Then("the user gets a Null pointer exception$")
    public void null_exception_thrown() {
        boolean result = false;
        try {
              taskCreater.createTask(null);
        } catch (NullPointerException e) {

            result = true;
         }
      assertTrue(result);
    }

这很丑!!

推荐答案

测试非快乐路径可能很难。这是一个很好的方式,我发现用黄瓜做。

Testing the not-happy-path can be hard. Here's a nice way that I've found to do it with cucumber.

Scenario: Doing something illegal should land you in jail
    Then a failure is expected
    When you attempt something illegal
    And it fails.

好的,不要拍我,因为我把然后 之前,我认为它读得更好但你不必这样做。

OK, don't shoot me because I put the Then before the When, I just think it reads better but you don't have to do that.

我将我的例外存储在(黄瓜范围的)世界对象中,但您也可以在步骤文件中执行此操作,但这将限制您以后。

I store my excepions in a (cucumber-scoped) world object, but you could also do it in your step file, but this will limit you later.

public class MyWorld {
    private boolean expectException;
    private List<RuntimeException> exceptions = new ArrayList<>();

    public void expectException() {
        expectException = true;
    }

    public void add(RuntimeException e) {
        if (!expectException) {
            throw e;
        }
        exceptions.add(e);
    }

    public List<RuntimeException> getExceptions() {
        return exceptions;
    }
}

您的步骤非常简单:

@Then("a failure is expected")
public void a_failure_is_expected() {
    myWorld.expectException();
}

在您(至少有时)期待异常的步骤中,捕获然后将它添加到世界。

In a step where you are (at least sometimes) expecting an exception, catch it and add it to the world.

@When("you attempt something illegal")
public void you_attempt_something_illegal() {
    try {
        myService.doSomethingBad();
    } catch (RuntimeException e) {
        world.add(e);
    }
}

现在你可以检查是否在世界。

Now you can check whether the exception was recorded in the world.

@And("it fails")
public void it_fails() {
    assertThat(world.getExceptions(), is(not(empty()));
}

这种方法最有价值的地方在于,当你不期望它时,它不会吞下异常。

The most valuable thing about this approach is that it won't swallow an exception when you don't expect it.

这篇关于Cucumber JVM:测试是否抛出了正确的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆