java - Asserting Correct Console Output in JUnit -


I am trying to write a basic JUnit tutorial, and I use a basic service layer using a simple Hello World example. Showing this with. My point is that even if the test output matches my control data, still believe that the false are still returning. Can you explain why assertEquals is not working, and show how can I fix it?

JUnit error

  org.junit.ComparisonFailure: expected: & lt; Hello World [] & gt; But it was: & lt; Hello World [] & gt;   

and my code test sample

  package com.springtutorial.mavenhelloworld.service; Import static org.junit.Assert. *; Import org.junit.Before; Import org.junit.BeforeClass; Import org.junit.Rule; Import org.junit.Test; Import org.junit.contrib.java.lang.system.StandardOutputStreamLog; Import com.springtutorial.junitandmavenhelloworld.service.HelloWorldMessage; Import com Source: import com.springtutorial.junitandmavenhelloworld.service.HelloWorldMessageService; Import com Source: Public category HelloWorldMessageServiceImplementationTest {static String controlMessageContent; Fixed HelloWorldMessage controlMessage; HelloWorld Message Test Message; HelloWorldMessageService testMessageService; @Royal PublicFirst StandardOutputStream Log Examination Trial = New standardoutputstream log (); @BeforeClass Public Static Zero setUpBeforeClass () throws exception {controlMessageContent = new string ("Hello World"); ControlMessage = New HelloWell Message Implementation (ControlMessage Content); } Public Unless Setup () before @ throws exception {testMessage = new HelloWell Message Implementation ("Hello World"); TestMessageService = New hello message service implementation (test message); } @Test Public Blank TestGet Message () {assertEquals (testMessage, testMessageService.getMessage ()); } @Test Public Zero TestSatmas () {testMessageService.setMessage (New hello message implementation ("Test Message")); AssertEquals ("test message", testMessageService.getMessage (). GetMessageAsString ()); } @Test Public Zero Test for each messageconconsole () {testMessageService.printMessageToConsole (); Emphasis (control message content, testlog.getlag ()); }}   

My HelloWorld Message Service Implementation Code

  Package Com. SpringTutorial.GenandWhenHolvoire Service; Public category HelloWorldMessageServiceImplementation applies HelloWorldMessageService {HelloWorldMessage Message; Public hallowal messaging implementation (hello message message) {super (); This.setMessage (newMessage); } Public HelloWorldMessage getMessage () {this.message return; } Public Zero Set Message (Hello Message Message New Message) {this.message = newMessage; } Public Zero Print Message Token () {System.out.println (this.message.getMessageAsString ()); }}    

issue is the use of println () < In the Code> HelloWorldMessageServiceImpletionation category This function adds a carriage return \ n at the end of the string, due to which the output does not match the control data. Either change it to print () method, or add a carrier return at the end of the test string.

  controlMessageContent = new string ("Hello World \ n");  

The correct way to add a carriage return on test control data is like this.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -