JUnit5的assertAll [英] JUnit5 assertAll

查看:214
本文介绍了JUnit5的assertAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下所示.我希望它去测试keyNames的所有元素.但是,如果任何测试失败并且不会遍历所有数组元素,它将停止.我的理解是,assertAll中的所有断言均已执行,所有失败都应一起报告.

The code is as shown below. I want it go test all the elements of keyNames. But, it stops if any test fails and doesn't iterate through all array elements. My understanding being, in assertAll all assertions are executed, and any failures should be reported together.

感谢您的时间和帮助.

private void validateData(SearchHit searchResult, String [] line){

        for(Integer key : keyNames){
            String expectedValue = getExpectedValue(line, key);
            String elementName = mappingProperties.getProperty(key.toString());

            if (elementName != null && elementName.contains(HEADER)){
                assertAll(
                        () -> assumingThat((expectedValue != null && expectedValue.length() > 0),
                                () -> {
                                        String actualValue = testHelper.getHeaderData(searchResult, elementName);

                                        if(expectedValue != null) {
                                            assertEquals(expectedValue, actualValue, " ###Element Name -> " + elementName +" :  Excepted Value ### " + expectedValue + " ### Actual Value ###" + actualValue);
                                        }
                                      }
                                 )
                );

            }

        }
    }

推荐答案

Assertions.assertAll()的Javadoc指出:

The javadoc of Assertions.assertAll() states :

断言所有提供的可执行文件均不会引发异常.

Asserts that all supplied executables do not throw exceptions.

实际上,您每次迭代时在 assertAll()中提供一个 Executable .
因此,循环的任何迭代失败都会终止测试执行.

And actually you provide a single Executable in assertAll() at each iteration.
So a failure in any iteration of the loop terminates the test execution.

实际上,您通过每次最多请求一个断言来多次调用 assertAll():

In fact you invoke multiple times assertAll() by requesting at most a single assertion at each time :

if(expectedValue != null) {
    assertEquals(expectedValue, actualValue, " ###Element Name -> " + elementName +" :  Excepted Value ### " + expectedValue + " ### Actual Value ###" + actualValue);
}

您想要做的相反:通过传递执行所需断言的多个 Executable 实例来调用 assertAll().

What you want is doing the reverse : invoking assertAll() by passing multiple Executable instances performing the required assertions.

因此,您可以将它们收集在带有经典循环的 List 中,并以这种方式传递它: assertAll(list.stream())或创建 Stream< Executable> ,而不进行任何收集并直接传递它,例如 assertAll(stream).

So you could collect them in a List with a classic loop and pass it in this way : assertAll(list.stream()) or creating a Stream<Executable> without any collect and pass it directly such as assertAll(stream).

这里是带有Stream的版本(完全未经测试),但是您应该了解一下:

Here is a version with Stream (not tested at all) but you should get the idea :

Stream<Executable> executables = 
keyNames.stream()
        .map(key-> 
               // create an executable for each value of the streamed list
                ()-> {
                        String expectedValue = getExpectedValue(line, key);
                        String elementName = mappingProperties.getProperty(key.toString());

                        if (elementName != null && elementName.contains(HEADER)){
                             assumingThat((expectedValue != null && expectedValue.length() > 0),
                                            () -> {                                             
                                                    String actualValue = testHelper.getHeaderData(searchResult, elementName);
                                                    if(expectedValue != null) {
                                                        assertEquals(expectedValue, actualValue, " ###Element Name -> " + elementName +" :  Excepted Value ### " + expectedValue + " ### Actual Value ###" + actualValue);
                                                    }                                                                            

                                            }
                            );

                        }

                    }
            );
Assertions.assertAll(executables);

这篇关于JUnit5的assertAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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