使用 JUNIT5 测试用例处理多个异常

Handling multiple exception using JUNIT5 test cases

我已经为我的 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 等...

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

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

}

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

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

@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 的命名约定,即成员、变量和参数采用小驼峰式。