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
.
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
?