0

So, in brief:

I have a method that is void, and prints stuff to standard output.

I have a second file that tests the output of functions against what it should be and returns true if they all pass.

I have a makefile that checks the output of the test file to make sure that all the tests passed.

My problem is that I don't know how to compare the void method's printed output against what it should be in the test file. I was told to modify the make file, but I don't know how. My other tests for methods with return types look something like this:

private static boolean testNumFunc() {
    if (MainFile.numFunc(300) == /*proper int output*/) {
        return true;
    }
    return false;
}

How would I test a void function this way by modifying the makefile?

2 Answers2

1

You could wrap the stdout in your own wrapper and read the results...

For example...

public class StreamCapturer extends OutputStream {

        private PrintStream old;

        public StreamCapturer(PrintStream old) {
            this.old = old;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            // Process the output here...
            // Echo the output back to the parent stream...
            old.print(c);
        }        
    }    
}

Then you would simply initialise it using something like...

System.setOut(new PrintStream(new StreamCapturer(System.out)));
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can pipe the output of the system.out to be read in some other aspect of your application.This is one way of doing it.There are many methods of achieving this task.it all depends on how you want it in your application

Gowrav
  • 289
  • 1
  • 4
  • 20