2013-03-21 1 views
0

기본 JUnit 튜토리얼을 작성하려고하는데 기본 서비스 레이어를 사용하는 간단한 Hello World 예제를 사용하여이를 보여줍니다. 내 문제는 테스트 출력이 내 컨트롤 데이터와 일치하더라도 assertEquals가 여전히 false를 반환한다는 것입니다. assertEquals가 작동하지 않는 이유를 설명하고이를 해결할 수있는 방법을 보여줄 수 있습니까?JUnit에서 올바른 콘솔 출력 지정하기

의 JUnit 오류

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

그리고 내 코드 테스트 샘플

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()); 
    } 
} 

내 HelloWorldMessageServiceImplementation 코드

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()); 
    } 

} 

답변

0

문제는 HelloWorldMessageServiceImplemenation 클래스 println()의 사용이다. 이 함수는 출력이 제어 데이터와 일치하지 않게하는 문자열의 끝에 캐리지 리턴 \n을 추가합니다. 이 값을 print() 메서드로 변경하거나 테스트 문자열의 끝에 캐리지 리턴을 추가하십시오.

테스트 제어 데이터에 캐리지 리턴을 추가하는 정확한 방법

controlMessageContent = new String("Hello World\n"); 
다음