簡體   English   中英

在JUnit中聲明正確的控制台輸出

[英]Asserting Correct Console Output in JUnit

我試圖編寫一個基本的JUnit教程,並通過一個使用基本服務層的簡單Hello World示例進行演示。 我的問題是,即使測試輸出與我的控制數據匹配,assertEquals仍然返回false。 您能解釋為什么assertEquals無法正常工作,並說明如何解決此問題?

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

}

問題是在HelloWorldMessageServiceImplemenation類中使用了println() 此函數在字符串的末尾添加一個回車符\\n ,導致輸出與控制數據不匹配。 將此更改為print()方法,或在測試字符串的末尾添加回車符。

下面是將回車符添加到測試控制數據的正確方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM