Вопрос

I'm trying to check console output by redirecting the standard output to a ByteArrayOutputStream object. I found this little code snipped that lets me do just that. However, usage of characters such as "-","+" etc. fail the tests. I was wondering why:

Here's the jUnit Test:

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//This test is try and test output against the console output,
//but the inclusion of characters '-', '*', '+', and '/' makes
//it especially hard to implement correctly. So abandoning
public class ApplicationTest {
    private final ByteArrayOutputStream outContent=new ByteArrayOutputStream();

    @Before
    public void setUpStreams(){
        System.setOut(new PrintStream(outContent));     
    }

    @After
    public void cleanUpStream(){
        System.setOut(null);
    }

    @Test
    public void test_Test1() {
        //Lexer lex=new Lexer("a+b*c");
        //System.out.print("a b c * + ");
        System.out.format("%s ", "a");
        System.out.format("%s ", "- ");
        String str="a - ";      

                //Test Fails.                       
        assertEquals(outContent.toString(),str);
    }

}
Это было полезно?

Решение

You have an extra space in the output.

System.out.format("%s ", "a");
System.out.format("%s ", "- ");
//                         ^
String str="a - ";      
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top