I want to be able to ensure that my print is exactly as expected, so I'm wondering if there's some kind of function in java or junit that will catch what system.out.print uses so I can perform some assertions on it.

Thanks for your help.

Recommended Answers

All 9 Replies

The System.out.print function takes a String parameter, so you should be able to perform assertions on the string that you are passing to it.

The System.out.print function takes a String parameter, so you should be able to perform assertions on the string that you are passing to it.

Well it's being passed in another class. How do I access it using my junit class?

For example, I have a class that just prints "hello world" in the main using System.out.println("hello world");

How do I assert from another class that it is indeed hello world being printed? Obviously it is, but I need to be able to check :(

Is it even possible without making additional code to take in the strings it's printing?

You can redirect System.out to a PrintStream of your own choice, so you could redirect to a byte array output stream from which you can check everything.

You can redirect System.out to a PrintStream of your own choice, so you could redirect to a byte array output stream from which you can check everything.

Can you provide an example? I'm looking at the javadoc for PrintStream and unsure of how to use it. It looks like it needs an OutputStream, which is an abstract class...

Yes, it needs some kind of OutputStream, such as ByteArrayOutputStream (the JavaDoc for OutputStream gives you a list of its known subclasses).
ps I don't know that is necessarily going to solve your problem - it's just an idea.

Yes, it needs some kind of OutputStream, such as ByteArrayOutputStream (the JavaDoc for OutputStream gives you a list of its known subclasses).
ps I don't know that is necessarily going to solve your problem - it's just an idea.

This is what I did.

System.setOut(myPrintStream);
System.out.println("hello world"); // doesn't print to screen cuz it's sent to printstream
System.setOut(System.out);
// this doesn't seem to be reassigning output stream to the normal one


How do I make it so I can reassign System's output back to the console? Right now, it still prints nothing so I'm guessing it's still sent to print stream...

Thanks! Making good progress.

Keep a reference to the original PrintStream before you reassign it. Then you can just swap it back

PrintStream defaultOutstream = System.out;
System.setOut(myPrintStream);
System.out.println("hello world"); // doesn't print to screen cuz it's sent to printstream
System.setOut(defaultOutstream);

Or maybe something similar to
System.console().writer(); ?

Or you could pipe the output to a file and use some tool to analyse that file after you're done running the application.
No modifications of your application code needed that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.