[英]More verbose output for JUnit assertEquals when comparing big strings
I just finished homework assignment and it passed automatic evaluation at the university. 我刚完成作业,并在大学通过了自动评估。 I thought of making unit tests for it, now that I know it's correct, to practice using
JUnit
. 现在,我知道这是正确的,我想为此进行单元测试,以练习使用
JUnit
。
I learned how to override and read standard output and simulate standard input . 我学习了如何覆盖和读取标准输出以及如何模拟标准输入 。 I wrote myself a method:
我给自己写了一个方法:
public static void simulateIn(final String str) {
// Simulated STDIN
final ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());
System.setIn(in);
}
I use it as follows: 我使用它如下:
@Test
public void TestOnePlusOne() throws Exception {
simulateIn("1 1 1 1");
// Homework assignment executes here
new Lab01().homework();
// Expected input. I didn't translate it, but it's just a simple calculator
assertEquals("Vyber operaci (1-soucet, 2-rozdil, 3-soucin, 4-podil):\n" +
"Zadej scitanec: \n" +
"Zadej scitanec: \n" +
"Zadej pocet desetinnych mist: \n" +
"1.0 + 1.0 = 2.0\n", outContent.toString());
}
I left the expected output untranslated, it really doesn't matter what it says right now. 我没有保留预期的输出,现在所说的真的没关系。 What matters is that the test fails and all I get is this:
重要的是测试失败了,我得到的只是:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running test.TestCalculator
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.097 sec <<< FAILURE!
Results :
Failed tests: TestOnePlusOne(test.TestCalculator): expected:<...desetinnych mist:
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
That's extremely unhelpful. 那是无济于事的。 Since I know the assignment is correct, this is something wrong with the unit test.
因为我知道分配是正确的,所以单元测试有问题。 Can I get the full diff of the strings that failed the
assertEquals
? 我可以获取未通过
assertEquals
的字符串的完整差异吗?
Trying to reproduce the same failure , I gave this a try with -- 为了重现同样的失败,我尝试了-
@Test
public void log() {
Assert.assertEquals("Vyber operaci (1-soucet, 2-rozdil, 3-soucin, 4-podil):\n" +
"Zadej scitanec: \n" +
"Zadej scitanec: \n" +
"Zadej pocet desetinnych mist: \n" +
"1.0 + 1.0 = 2.0\n", ""); // empty string to make sure this fails
}
Executing command mvn clean install
from the command line at the root directory on the module containing the above test class. 从包含上述测试类的模块的根目录中的命令行执行命令
mvn clean install
。 It resulted in the following logs - 结果为以下日志-
Running TestOne true Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.356 sec <<< FAILURE! - in TestOne log on log(TestOne)(TestOne) Time elapsed: 0.007 sec <<< FAILURE! org.junit.ComparisonFailure: expected:<[Vyber operaci (1-soucet, 2-rozdil, 3-soucin, 4-podil): Zadej scitanec: Zadej scitanec: Zadej pocet desetinnych mist: 1.0 + 1.0 = 2.0 ]> but was:<[]> at TestOne.log(TestOne.java:17)
Notice few details in the logs - 注意日志中的一些详细信息-
Just to ensure consistency, I've used Apache Maven 3.3.9
and maven-surefire-plugin:2.19.1
为了确保一致性,我使用了
Apache Maven 3.3.9
和maven-surefire-plugin:2.19.1
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</build>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.