为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查JUnit我们期望返回的确切值?

如何检查JUnit我们期望返回的确切值?

饮歌长啸 2019-04-18 14:15:12
在下面的示例中,可以检查工作表的最后一个元素是否真的没有出现,因为它已经在列表中。如何查看预期返回的确切值?public class streamExample2 {public static void main(String[] args) {     List<String> stringList = new ArrayList<String>();     stringList.add("один");     stringList.add("два");     stringList.add("три");     stringList.add("один");     System.out.println (countstring(stringList));}     public static List<String> countstring  (List <String> stringList){         Stream <String> stream = stringList.stream ();             List<String>differentStrings = stream .distinct ()             .collect (Collectors.toList ());         return differentStrings;     }    }
查看完整描述

2 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

您可以使用JUnit轻松测试具有返回值的方法。测试a void main在某种程度上更难,并且在更大的应用程序中没有任何意义(那些类比包含更多类的应用程序更多main)。

在你的情况下,我会将要测试的代码提取到一个方法中,让我们说下面的一个:

import java.util.List;import java.util.stream.Collectors;public class StackoverflowDemo {

    public static List<String> getDistinctValuesFrom(List<String> list) {
        return list.stream().distinct().collect(Collectors.toList());
    }}

由于这种方法static,您不需要任何类的实例。

对于简单的单元测试 - 通常 - 您需要输入值和预期输出值。在这种情况下,您可以实现两个列表,一个列表具有重复项,另一个列表表示消除第一个列表重复项的预期结果。
一个JUnit测试用例将预期的输出与/用来比较(我将永远不会用英语 - 这里的母语人士编辑这些介词)实际的输出。

JUnit使用比较(返回)值(方法)的特定方法。
测试此方法的测试类可能如下所示:

import static org.junit.jupiter.api.Assertions.*;import java.util.ArrayList;import java.util.List;import org.junit.jupiter.api.
Test;import de.os.prodefacto.StackoverflowDemo;class StreamTest {

    @Test
    void test() {
        // provide a list that contains dpulicates (input value)
        List<String> input = new ArrayList<String>();
        input.add("AAA");
        input.add("BBB");
        input.add("CCC");
        input.add("AAA");
        input.add("DDD");
        input.add("EEE");
        input.add("AAA");
        input.add("BBB");
        input.add("FFF");
        input.add("GGG");

        // provide an expected result
        List<String> expected = new ArrayList<String>();
        expected.add("AAA");
        expected.add("BBB");
        expected.add("CCC");
        expected.add("DDD");
        expected.add("EEE");
        expected.add("FFF");
        expected.add("GGG");

        // get the actual value of the (static) method with the input as argument
        List<String> actual = StackoverflowDemo.getDistinctValuesFrom(input);
        // assert the result of the test (here: equal)
        assertEquals(expected, actual);
    }}

请注意,您可以而且应该测试不良行为,例如误报或Exceptions。对于比这个简单示例更进一步的内容,谷歌搜索JUnit教程并阅读其中的一些内容。

请注意,测试用例也可能是错误的,这可能会导致严重的问题!仔细检查您的测试,因为预期值可能是错误的,因此尽管方法正确实施,测试失败的原因。


查看完整回答
反对 回复 2019-05-15
?
慕码人2483693

TA贡献1860条经验 获得超9个赞

这可以通过HashSet完成。HashSet是一种仅存储唯一值的数据结构。

@Testpublic void testSalutationMessage() {

    List<String> stringList = new ArrayList<String>();

    stringList.add("one");
    stringList.add("two");
    stringList.add("three");
    stringList.add("one");
    Set<String> set = new HashSet<String>();

    stringList.stream().forEach(currentElement -> {
        assertFalse("String already exist in List", set.contains(currentElement));
        set.add(currentElement);
    });}


查看完整回答
反对 回复 2019-05-15
  • 2 回答
  • 0 关注
  • 835 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号