Cucumber JVM:测试是否抛出了正确的异常

问题描述 投票:16回答:7

如何在使用Cucumber JVM时测试是否抛出了正确的异常?使用JUnit时,我会做这样的事情:

@Test(expected = NullPointerException.class)
public void testExceptionThrown(){
    taskCreater.createTask(null);
}

如您所见,这非常优雅。但是,当使用黄瓜JVM时,我怎样才能达到同样的优雅?我的测试现在看起来像这样:

@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);
}

请注意需要一个try..catch,然后是旗帜上的assertTrue

cucumber-jvm
7个回答
11
投票

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

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

好吧,不要拍我,因为我把Then放在When之前,我认为它看起来更好,但你不必这样做。

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

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();
}

在您(至少有时)期待异常的步骤中,抓住它并将其添加到世界中。

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

现在,您可以检查是否在世界中记录了异常。

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

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


7
投票

您是否尝试过使用带有ExpectedException的junit @Rule注释,如下所示:

@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Then("the user gets a Null pointer exception$")
public void null_exception_thrown() {
    expectedEx.expect(NullPointerException.class);
    //expectedEx.expectMessage("the message");
    taskCreater.createTask(null);
}

5
投票

行为测试验证您的项目是否遵循规范,我怀疑用户在使用系统时是否期望NullPointerException。

在我看来(我不熟悉你的项目),只应在单元测试期间检查异常,因为它们对应于意外错误或用户错误。

在行为测试期间检查异常是非常不寻常的。如果在测试期间抛出异常,则应该失败。

例如:

test.feature

Given that I have a file "users.txt"
And I try to import users /* If an Exception is thrown here, the test fails */
Then I should see the following users: /* list of users */

在我的单元测试中,我会:

@Test(expected = MyException.class)
public void importUsersShouldThrowMyExceptionWhenTheFileIsNotFound() {
    // mock a call to a file and throw an exception
    Mockito.when(reader.readFile("file.txt").thenThrow(new FileNotFoundException());

    importer.importUsers();
}

3
投票

从未使用黄瓜,但会

    public void null_exception_thrown() {
            try {
                  taskCreater.createTask(null);
                  fail("Null Pointer Expected");
            } catch (NullPointerException e) {

                // Do Nothing
             }
        }

为你工作?


1
投票

我相信Cucumber的目的是测试更高级别的验收测试,而不是低级别的单元测试,因为在这种情况下,我们将测试与不是Cucumber框架所需用法的行为相反的结构。


1
投票

我建议你使用org.assertj.assertj-core

感谢Assertions类,您可以简化下面的断言:

@Then("the user gets a Null pointer exception$")
public void null_exception_thrown() {
    Assertions.assertThatThrownBy(() -> taskCreater.createTask(null)).
               isInstanceOf(NullPointerException.class);
}

1
投票

我以前在功能步骤中有预期的异常名称。对于计算器测试示例

这是我的除法异常的功能文件条目:

Scenario: Dividing a number with ZERO
Given I want to test calculator
When I insert 5.5 and 0.0 for division
Then I got Exception ArithmeticException

所以,您可能会看到,我添加了例外名称。因此,在步骤定义中,我得到了异常的名称。

现在,我们需要在除以ZERO并输入变量时获得异常。并获取此变量以比较其类名(异常名称)

所以,在步骤定义中,除以零

private Exception actualException;
 @When("^I insert (.+) and (.+)for division$")
public void iInsertAndForDivision(double arg0, double arg1) throws Throwable {
    try {
        result = calculator.div(arg0, arg1);
    } catch (Exception e) {
        actualException =e;
    }
}

我需要步骤定义来验证异常

  @Then("^I got Exception (.+)")
public void iGotArithmeticException(String aException) throws Throwable {        
Assert.assertEquals("Exception MissMatch",aException,actualException.getClass().getSimpleName());
}

完整的项目可以在这里看到:Steps for cucumber

© www.soinside.com 2019 - 2024. All rights reserved.