Java 如何签入JUnit我们希望返回的值到底是什么?

Java 如何签入JUnit我们希望返回的值到底是什么?,java,Java,在下面的示例中,可以检查图纸的最后一个元素是否确实出现,因为它已经在列表中。如何检查预期返回的确切值 public class streamExample2 { public static void main(String[] args) { List<String> stringList = new ArrayList<String>(); stringList.add("один"); stringList.add("два");

在下面的示例中,可以检查图纸的最后一个元素是否确实出现,因为它已经在列表中。如何检查预期返回的确切值

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{
公共静态void main(字符串[]args){
List stringList=新建ArrayList();
字符串列表。添加(“ааа”);
stringList.添加(“ааa”);
stringList.添加(“ццц”);
字符串列表。添加(“ааа”);
System.out.println(countstring(stringList));
}
公共静态列表countstring(列表stringList){
Stream=stringList.Stream();
ListDifferentString=stream.distinct()
.collect(Collectors.toList());
返回不同的字符串;
}
}

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

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

}
@测试
public void testsaltationmessage(){
List stringList=新建ArrayList();
字符串列表。添加(“一”);
添加(“两个”);
添加(“三”);
字符串列表。添加(“一”);
Set=newhashset();
stringList.stream().forEach(currentElement->{
assertFalse(“列表中已存在字符串”,set.contains(currentElement));
set.add(currentElement);
});
}

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

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

}
@测试
public void testsaltationmessage(){
List stringList=新建ArrayList();
字符串列表。添加(“一”);
添加(“两个”);
添加(“三”);
字符串列表。添加(“一”);
Set=newhashset();
stringList.stream().forEach(currentElement->{
assertFalse(“列表中已存在字符串”,set.contains(currentElement));
set.add(currentElement);
});
}

您可以使用JUnit轻松测试具有返回值的方法。测试
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());
    }
}
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);
    }
}
请注意,您可以也应该测试不希望出现的行为,如误报或异常。对于任何比这个简单示例更进一步的内容,请使用谷歌搜索JUnit教程并阅读其中的一些

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


您可以使用JUnit轻松测试具有返回值的方法。测试
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());
    }
}
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);
    }
}
请注意,您可以也应该测试不希望出现的行为,如误报或异常。对于任何比这个简单示例更进一步的内容,请使用谷歌搜索JUnit教程并阅读其中的一些

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


您是否尝试过实现JUnit测试用例?如果是,请在这里显示它…单元测试void方法比测试返回值的方法要困难得多。我建议将构造您要测试的值的东西拉到一个方法中?如果是,请在这里显示它…单元测试void方法比测试返回值的方法要困难得多。我建议将构造您要测试的值的东西拉到一个方法中。我不喜欢这样做,因为执行测试目标和断言目标返回的结果之间没有明确的区别。在我看来,这使得测试很难理解和维护。我不喜欢这样,因为在执行测试目标和断言目标返回的结果之间没有明确的区别。在我看来,这使得测试很难理解和维持。