0

I am trying to write a basic JUnit tutorial, and I am demonstrating this with a simple Hello World example using a basic service layer. My issue is that even though the test output matches my control data, assertEquals is still returning false. Can you explain why assertEquals isn't working, and show how I can fix this?

The JUnit error

org.junit.ComparisonFailure: expected:<Hello World[]> but was:<Hello World[
]>

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.springtutorial.junitandmavenhelloworld.service.HelloWorldMessageImplementation;
import com.springtutorial.junitandmavenhelloworld.service.HelloWorldMessageService;
import com.springtutorial.junitandmavenhelloworld.service.HelloWorldMessageServiceImplementation;

public class HelloWorldMessageServiceImplementationTest
{
    static String controlMessageContent;
    static HelloWorldMessage controlMessage;

    HelloWorldMessage testMessage;
    HelloWorldMessageService testMessageService;

    @Rule
    public final StandardOutputStreamLog testLog = new StandardOutputStreamLog();

    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
    controlMessageContent = new String("Hello World");
    controlMessage = new HelloWorldMessageImplementation(controlMessageContent);
    }

    @Before
    public void setUp() throws Exception
    {
    testMessage = new HelloWorldMessageImplementation("Hello World");
    testMessageService = new HelloWorldMessageServiceImplementation(testMessage);
    }

    @Test
    public void testGetMessage()
    {
    assertEquals(testMessage, testMessageService.getMessage());
    }

    @Test
    public void testSetMessage()
    {
    testMessageService.setMessage(new HelloWorldMessageImplementation("Test Message"));
    assertEquals("Test Message", testMessageService.getMessage().getMessageAsString());
    }

    @Test
    public void testPrintMessageToConsole()
    {
    testMessageService.printMessageToConsole();
    assertEquals(controlMessageContent, testLog.getLog());
    }
}

My HelloWorldMessageServiceImplementation code

package com.springtutorial.junitandmavenhelloworld.service;

public class HelloWorldMessageServiceImplementation implements
    HelloWorldMessageService
{
    HelloWorldMessage message;

    public HelloWorldMessageServiceImplementation(HelloWorldMessage newMessage)
    {
    super();
    this.setMessage(newMessage);
    }

    public HelloWorldMessage getMessage()
    {
    return this.message;
    }

    public void setMessage(HelloWorldMessage newMessage)
    {
    this.message = newMessage;
    }

    public void printMessageToConsole()
    {
    System.out.println(this.message.getMessageAsString());
    }

}
cyotee doge
  • 1,128
  • 4
  • 15
  • 33

1 Answers1

0

The issue is the use of println() in the HelloWorldMessageServiceImplemenation class. This function adds a carriage return \n to the end of the string that causes the output to not match the control data. Either change this to the print() method, or add a carriage return to the end of the test string.

The correct way to add a carriage return to the test control data follows

controlMessageContent = new String("Hello World\n");
cyotee doge
  • 1,128
  • 4
  • 15
  • 33