【问题标题】:Handling multiple exception using JUNIT5 test cases使用 JUNIT5 测试用例处理多个异常
【发布时间】:2020-02-11 09:24:51
【问题描述】:

我为我的 DynamoDB CRUD 操作创建了一个 JUNIT 测试用例。我在下面的代码中使用了断言来处理意外的异常提供者。

@Test
void createItemsInTable() throws Exception {

    Assertions.assertThrows(UnexpectedException.class, () -> {

        ResponseEntity Res;

        CatalogItems cat = new CatalogItems(3, "", "fghsdfh");

        Res = Eservice.createItemInDynamoDbTable(cat);

        System.out.println(Res.getStatusCodeValue());

        CatalogItems cat2 = Eservice.findById("3");

        assertEquals(Res.getStatusCodeValue(), 201);

        assertEquals(cat2.getTitle(), cat.getTitle());

        assertEquals(cat2.getAuthor(), cat.getAuthor());

        assertEquals(cat2.getId(), cat.getId());

    });

}

我应该如何在上面的 JUNIT 测试用例中指定多个异常,例如:UnexpectedException、ResourceNotFoundException 等...

【问题讨论】:

  • 一段代码,即lambda表达式,只会抛出一个异常。因此,您应该指定在该特定情况下您期望的那个。也就是说,您的代码看起来好像根本不会出现异常,为什么要尝试断言其他内容?
  • 嗨 Johan,感谢您的更新,我已经给出了更新 AWS dynamoDb 表中项目的完整代码。请检查我的代码并回答我的问题?
  • 如有任何澄清,请发布您的 cmets。提前致谢

标签: spring-boot amazon-dynamodb junit5 spring-boot-test


【解决方案1】:

我会将这三种不同的场景分成各自的测试用例。类似的:

@Test
void updateItem() throws Exception {
    CatalogItems cat = new CatalogItems(1, "xxxx", "f");

    ResponseEntity Res = Eservice.updateItemInDynamoDbTable(cat);

    System.out.println(Res.getStatusCodeValue());

    CatalogItems cat2 = Eservice.findById("1");

    assertEquals(Res.getStatusCodeValue(), 201);

    assertEquals(cat2.getAuthor(), cat.getAuthor());

    assertEquals(cat2.getTitle(), cat.getTitle());

    assertEquals(cat2.getId(), cat.getId());
}

@Test
void updateItemFailsWhenDestinationIsNotFound() {
    CatalogItems cat = new CatalogItems(3, "xxx", "xxx");

    Exception exception = assertThrows(ResourceNotFoundException.class, () -> {
        ResponseEntity Res = Eservice.updateItemInDynamoDbTable(cat);
    });

    String expectedMessage = "ResourceNotFoundException";

    String actualMessage = exception.getMessage();

    assertTrue(actualMessage.contains(expectedMessage));
}

@Test
void updateItemFailsWhenIdIsNotAvailableAtDestination() {
    CatalogItems cat = new CatalogItems(3, "xxx", "xxx");

    Exception exception = assertThrows(NullPointerException.class, () -> {
        ResponseEntity Res = Eservice.updateItemInDynamoDbTable(cat);
    });

    System.out.println(exception.getMessage());

    assertNull(exception.getMessage());
}

最好只包含实际引发异常的代码 在他 assertThrows lambda 中。这样你就可以确定是这段代码失败了。

我还建议坚持 Java 的命名约定,即将成员、变量和参数使用小写字母。

【讨论】:

    【解决方案2】:

    您好,我在下面给出了正面和负面情况的代码,我创建了一组不同的代码。在运行测试用例时,我正在评论不需要的代码并运行我的文本用例。例如:如果我必须测试我的正面测试用例,我应该评论负面测试用例,反之亦然。

    @Test
    void updateItem() throws Exception {
    
        //Positive test case
    
        ResponseEntity Res;
    
        CatalogItems cat = new CatalogItems(1, "xxxx", "f");
    
        Res = Eservice.updateItemInDynamoDbTable(cat);
    
        System.out.println(Res.getStatusCodeValue());
    
        CatalogItems cat2 = Eservice.findById("1");
    
        assertEquals(Res.getStatusCodeValue(), 201);
    
        assertEquals(cat2.getAuthor(), cat.getAuthor());
    
        assertEquals(cat2.getTitle(), cat.getTitle());
    
        assertEquals(cat2.getId(), cat.getId());
    
    
    
        //Negative test case - if the destination is not found
    
            Exception exception = assertThrows(ResourceNotFoundException.class, () -> {
    
                ResponseEntity Res;
    
                CatalogItems cat = new CatalogItems(3, "xxx", "xxx");
    
                Res = Eservice.updateItemInDynamoDbTable(cat);
    
            });
    
            String expectedMessage = "ResourceNotFoundException";
    
            String actualMessage = exception.getMessage();
    
            assertTrue(actualMessage.contains(expectedMessage));
    
    
    
        //Negative test case for updating Id which is not available at destination
    
            Exception exception = assertThrows(NullPointerException.class, () -> {
    
                ResponseEntity Res;
    
                CatalogItems cat = new CatalogItems(3, "xxx", "xxx");
    
                Res = Eservice.updateItemInDynamoDbTable(cat);
    
            });
    
            System.out.println(exception.getMessage());
    
            assertNull(exception.getMessage());
    
    }
    

    您能否确认我准备和运行测试用例的方式没问题,否则必须改变测试方式?

    【讨论】:

    • 我会将否定测试用例提取到它自己的测试方法中。除此之外,它看起来还不错。
    • 我会将否定测试用例提取到它自己的测试方法中。能否请您更具体地说明此声明?
    • 这样会更有帮助
    猜你喜欢
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2020-09-21
    • 2019-06-22
    相关资源
    最近更新 更多