2016-03-31 27 views
6

Jestem stoi następujący scenariusz:assertEquals (String, String) ComparisonFailure gdy treść jest identyczna

Mam aplikacja, która wypluwa wszystkiego na standardowe wyjście (prosty test firma) i staram się tego JUnit .

Moim problemem jest to, kiedy uruchomić aplikację, to wraca do mnie w konsoli: (kopiowania i wklejania z IntelliJ)

Id 1234 nao encontrado 
123, R$ 441,00 
321, R$ -8490,00 
255, R$ 884,00 

druku:

enter image description here

A moja próba jest:

assertEquals(outContent.toString().trim(),"Id 1234 nao encontrado\n" + 
       "123, R$ 441,00\n" + 
       "321, R$ -8490,00\n" + 
       "255, R$ 884,00"); 

Otrzymuję:

junit.framework.ComparisonFailure: <Click to see difference> 


    at junit.framework.Assert.assertEquals(Assert.java:100) 
    at junit.framework.Assert.assertEquals(Assert.java:107) 
    at junit.framework.TestCase.assertEquals(TestCase.java:269) 
    at com.company.AccountManagerTest.testPrintAccountsBalance(AccountManagerTest.java:85) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at junit.framework.TestCase.runTest(TestCase.java:176) 
    at junit.framework.TestCase.runBare(TestCase.java:141) 
    at junit.framework.TestResult$1.protect(TestResult.java:122) 
    at junit.framework.TestResult.runProtected(TestResult.java:142) 
    at junit.framework.TestResult.run(TestResult.java:125) 
    at junit.framework.TestCase.run(TestCase.java:129) 
    at junit.framework.TestSuite.runTest(TestSuite.java:252) 
    at junit.framework.TestSuite.run(TestSuite.java:247) 
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

enter image description here

Więc, co robię źle tutaj?

Testowanie z JUnit4 i assertJ 2.4.0

Odpowiedz

16

Widoczne znaki są identyczne, ale znaki niedrukowalne nie są.

Porównywasz oczekiwane wyjście zawierające CRLF (\r\n) z rzeczywistą mocą wyjściową za pomocą LF (\n). Możesz to zobaczyć w IntelliJ po prawej stronie obu obszarów tekstowych.

Proste rozwiązanie polega na zastąpieniu \n w łańcuchu za pomocą \r\n. Lub usuń \r z drugiej.


Warto również zauważyć, że kolejność parametrów jest rzeczywiście (Object expected, Object actual), więc outContent powinien iść drugi ponieważ to jest „rzeczywista” produkcja.

+0

Dziękuję bardzo, działało! – Leonardo

+2

Jeśli nie jesteś pewien, czy ciąg znaków będzie miał '\ r', czy nie, innym rozwiązaniem jest użycie' outContent.toString(). Trim(). Replace ("\ r", "") 'jako rzeczywisty ciąg . Oznacza to, że przed porównaniem należy usunąć wszystkie znaki '\ r'. – ajb

+0

Jakikolwiek sposób debugowania tego, z wyjątkiem patrzenia na niego ręcznie? –