练习JUnit时比较字符串时出现奇怪行为

huangapple go评论71阅读模式
英文:

Practicing JUnit strange behavior comparing strings

问题

我第一次尝试使用JUnit,并且遇到了一个奇怪的错误:

方法上的测试是正常的,但是当我在主方法中尝试我的第一个测试时,它失败了,没有任何原因。这是测试的代码:

    @Test
    public void MyTestMain1(){
        String input = "1\n2\n2\n";        
        
        ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
        System.setIn(in);
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        
        String [] args = {};
        Demo.main(args);
        
        // 期望输出:
        String consoleOutput = "Enter side 1: \n";
        consoleOutput += "Enter side 2: \n";
        consoleOutput += "Enter side 3: \n";
        consoleOutput += "This is a triangle.\n";
        
        assertEquals(consoleOutput, out.toString());
    }

这是我的主类的代码:

public class Demo {

    public static void main(String[] args) {
        // 从System.in读取
        Scanner reader = new Scanner(System.in);  
        
        System.out.println("Enter side 1: ");
        // 将输入的下一个标记扫描为int。
        int side_1 = reader.nextInt();
        
        System.out.println("Enter side 2: ");
        // 将输入的下一个标记扫描为int。
        int side_2 = reader.nextInt();
        
        System.out.println("Enter side 3: ");
        // 将输入的下一个标记扫描为int。
        int side_3 = reader.nextInt();
        
        if (isTriangle(side_1, side_2, side_3)) {
            System.out.println("This is a triangle.");
        }
        else {
            System.out.println("This is not a triangle.");
        }
        
        reader.close();

    }
    
    public static boolean isTriangle(double a, double b, double c) {
        if ((a + b > c) &&
            (a + c > b) && // 应该是 a + c > b
            (b + c > a)) {
            return true; 
        }
        return false;
    }

}

这很简单,但它失败了,原因是一些奇怪的隐藏字符,我不知道它们来自哪里。

这是JUnit的输出:

练习JUnit时比较字符串时出现奇怪行为

如果我双击它,我看到的是:

练习JUnit时比较字符串时出现奇怪行为

在第一张图中,似乎有一个额外的 "\n",但在第二张图中没有。而且,如果我手动添加它,那么相同的错误将出现,尽管现在第二张图将显示有一个额外的 \n。

英文:

I have been trying JUnit for the first time and I am facing a strange error:

Tests on method are okay, but when I tried my first test in the main method, it fails without reason. This is the code for the test:

    @Test
	public void MyTestMain1(){
		String input = "1\n2\n2\n";		
		
		ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
		System.setIn(in);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		System.setOut(new PrintStream(out));
		
		String [] args = {};
		Demo.main(args);
		
		// expected output:
		String consoleOutput = "Enter side 1: \n";
		consoleOutput += "Enter side 2: \n";
		consoleOutput += "Enter side 3: \n";
		consoleOutput += "This is a triangle.\n";
		
		assertEquals(consoleOutput, out.toString());
	}

And here the code of my main class:

public class Demo {

    public static void main(String[] args) {
        // Reading from System.in
        Scanner reader = new Scanner(System.in);  
        
        System.out.println("Enter side 1: ");
        // Scans the next token of the input as an int.
        int side_1 = reader.nextInt();
        
        System.out.println("Enter side 2: ");
        // Scans the next token of the input as an int.
        int side_2 = reader.nextInt();
        
        System.out.println("Enter side 3: ");
        // Scans the next token of the input as an int.
        int side_3 = reader.nextInt();
        
        if (isTriangle(side_1, side_2, side_3)) {
            System.out.println("This is a triangle.");
        }
        else {
            System.out.println("This is not a triangle.");
        }
        
        reader.close();

    }
    
    public static boolean isTriangle(double a, double b, double c) {
        if ((a + b > c) &&
            (a + c > b) && // should be a + c > b
            (b + c > a)) {
            return true; 
        }
        return false;
    }

}

It is very simple, but it is failing and the reason is some strange hidden character that I don't know where is coming from.

Here is the output by JUnit:

练习JUnit时比较字符串时出现奇怪行为

And here what I see if I double click on it:

练习JUnit时比较字符串时出现奇怪行为

In the first image it appears to be an additional "\n" but in the second one it doesn't appear. Also, if I add it manually then the same error will appear, though now the second image will be the one showing that there is an additional \n.

答案1

得分: 0

Line separators are platform dependant. For example, For Windows, Line Separator means \r\n

\r - carriage return
\n - new line

The problem is that the expected String has \n only while when the String gets generated using Scanner, it has \r\n. Due to this both strings were not equal.

To solve this issue, you can add platform independent line separator through

System.lineSeparator()

Using this will ensure, the code doesn't break in other operating systems.

英文:

Line seperators are platform dependant. For example, For Windows, Line Separator means \r\n

\r - carriage return
\n - new line

The problem is that the expected String has \n only while when the String gets generated using Scanner, it has \r\n. Due to this both strings were not equal.

To solve this issue, you can add platform independent line separator through

System.lineSeparator()

Using this will ensure, the code doesn't break in other operating systems.

huangapple
  • 本文由 发表于 2020年8月2日 03:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63209228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定