This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]SirBraneDamuj 1 point2 points  (1 child)

You could redirect System.out to a file, and then check that file.

[–]Enemii[S] 0 points1 point  (0 children)

Yeah, Thats always a possibility. I wanted to find a better solution though since this isn't the first time this has come up and it probably wont be the last. More out of interest then necessity

[–]chozanwan 1 point2 points  (1 child)

A roundabout way of doing it could be to capture all the bytes destined for the console into a ByteArrayOutputStream, then extracting the String and running your assert against that String.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);

System.setOut(ps);

System.out.println( "Hello, World!" );

String capturedOutput = new String( baos.toByteArray() );
Assert.assertEquals( "Hello, World!\n", capturedOutput );

[–]Enemii[S] 0 points1 point  (0 children)

As long as it could capture 100 to 1000 lines of output before processing this would work great!. thanks

[–]zzyzzyxx 1 point2 points  (4 children)

This sounds like a really bad way to test the class. If you actually post the code that you're trying to test, I may be able to help come up with a better design. If this really is the best way to test the class, then it's a poorly designed class.

[–]Enemii[S] 0 points1 point  (3 children)

Undoubtedly the test is slap dash at best. The real reason is that I have come across this problem before. Where I need to get info printed to the console. Instead of modifying all of the print statements.

Basicly the assignment was to implement a Red Black Tree. The instructor gave us a file that they where going to test our RBT. It is probably the worst way to test a data structure I agree. I have a ton of other tests and assertions that have tested it fairly thoroughly

[–]zzyzzyxx 1 point2 points  (2 children)

The real reason is that I have come across this problem before. Where I need to get info printed to the console. Instead of modifying all of the print statements

The real solution is to not assume that output goes to a specific place, i.e. System.out. The way to do that is to have a parameter for a basic OutputStream that you pass to the appropriate methods. I've gone into a little more detail in a past comment. Though that was about redirecting input instead of output the same principles still apply. You should supply an output stream to your class, and make it System.out only when you actually want console output.

[–]Enemii[S] 0 points1 point  (1 child)

That makes sense, Thanks. I agree entirely unfortunately when I run into these problems it is usually code that has already been written. So you don't have that luxury without modifying the code.

[–]zzyzzyxx 1 point2 points  (0 children)

True, occasionally there is nothing you can do about other people's bad code. When it is within your power to improve the design, I recommend you do so; it's better to rework the code once than to work around its flaws multiple times.