0

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?

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • are you executing this using `mvn test` ? If so what maven version is in use? – Naman Mar 08 '17 at 16:29
  • I'm executing within netbeans. I'm not very experienced with this, so I honestly have no idea what commands do run in background. – Tomáš Zato Mar 08 '17 at 16:36

1 Answers1

0

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

  1. Running TestOne - test class name
  2. log(TestOne) - name of the method
  3. org.junit.ComparisonFailure type of failure
  4. expected and actual ..but was

Just to ensure consistency, I've used Apache Maven 3.3.9 and 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>
Naman
  • 27,789
  • 26
  • 218
  • 353
  • I couldn't run maven in the directory where tests are located. Running it in the project root produced the same output: http://pastebin.com/SMSviifP I have no plugins in pom.xml: https://gist.github.com/Darker/5ff47804ea1b8502583e95b2a4079b02#file-pom-xml Maven version: `Apache Maven 3.0.5` It's strange that you have newer version, I downloaded netbeans two weeks ago and maven was part of that download. – Tomáš Zato Mar 08 '17 at 17:28
  • @TomášZato updating the surefire-plugin to 2.19.1 shall help you. You need to include the `` tag at the same level as your dependencies tag. – Naman Mar 08 '17 at 17:35