Results 1 to 13 of 13
  1. #1
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default Capture Output of System.out

    Hi All,

    I am running a junit test case and using assertEquals to compare results.

    Program runs like this:

    Class M {

    public String m(){
    OutputStream os = os = new ByteArrayOutputStream();;
    PrintStream ps ;

    if (i=1)
    call Class A method();
    else
    call Class B method();

    String s=os.toString();
    }

    }

    Class A {
    void m(){
    ps = new PrintStream(os);
    System.out.println("a");
    System.setOut(ps);
    }

    }


    I expect the above code to capture "a" in System.out.println("a") into String s = os.toString();

    But its returning me an empty array.

    Where am i going wrong ?



    Venny

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Venny View Post
    Hi All,

    I am running a junit test case and using assertEquals to compare results.

    Program runs like this:

    Class M {

    public String m(){
    OutputStream os = os = new ByteArrayOutputStream();;
    PrintStream ps ;

    if (i=1)
    call Class A method();
    else
    call Class B method();

    String s=os.toString();
    }

    }

    Class A {
    void m(){
    ps = new PrintStream(os);
    System.out.println("a");
    System.setOut(ps);
    }

    }


    I expect the above code to capture "a" in System.out.println("a") into String s = os.toString();

    But its returning me an empty array.

    Where am i going wrong ?
    Before those methods print anything to System.out you should've changed it already. So before you call those methods do:

    Java Code:
    ByteArrayOutputStream baos= new ByteArrayOutputStream();
    System.setOut(new PrintStream(baos));
    
    // call your methods here
    
    String redirected= baos.toString();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    hey thanks..but this is the part which am not clear with. the code should go into class M or Class A ? how would the code know that i want "a" to be printed out through system.out.println. i mean how would "a" particularly go into baos ??


    Venny

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Venny View Post
    hey thanks..but this is the part which am not clear with. the code should go into class M or Class A ? how would the code know that i want "a" to be printed out through system.out.println. i mean how would "a" particularly go into baos ??
    In your case I'd put the setup of System.out in class M. Call the methods (in the other classes) that print to System.out (effectively ending up in baos) and at the end collect the results in class M again. The methods in the other classes don't know where their output ends up, all they know is that they are printing to System.out (which your class M has modified to print to baos).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    The code worked fine :) thanks for your help. I am able to capture the output but now problem here is when am running junit, its showing me a failure. here is what am doing.

    String actRes = baos.toString(); (Assume its returning b)
    String expRes = "b"
    assertEquals(actRes, expRes);

    the test should have been a success but unfortunately its not. even though the results are same. its showing actRes is b[] and expRes is b[ ]. What does that mean ?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Venny View Post
    The code worked fine :) thanks for your help. I am able to capture the output but now problem here is when am running junit, its showing me a failure. here is what am doing.

    String actRes = baos.toString(); (Assume its returning b)
    String expRes = "b"
    assertEquals(actRes, expRes);

    the test should have been a success but unfortunately its not. even though the results are same. its showing actRes is b[] and expRes is b[ ]. What does that mean ?
    In your original post I saw that you used System.out.println( ... ); note that this method emits an end-of-line sequence to your byte buffer. (notably \r\n for MS Windows, or \n for Unix). I don't know where those angular brackets come from (see your example above).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    sorry couldn't get what you mean :(

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Venny View Post
    sorry couldn't get what you mean :(
    Show us the code that prints output and I'll show you the resulting String.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Here is the code for Class M:

    package jUnit.code;

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


    class M {

    OutputStream os = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream (os);
    String s;

    public static void main(String [] argv){
    M obj = new M();
    if (argv.length > 0)
    obj.m(argv[0], argv.length);
    }

    public String m(String arg, int i) {


    int q = 1;
    A o = null;

    if (i == 0)
    q = 4;
    q++;
    switch (arg.length()) {
    case 0: q /= 2; break;
    case 1: o = new A(); q = 10; break;
    case 2: o = new B(); q = 5; break;
    case 3: o = new A(); new B(); q = 25; break;
    default: o = new B(); break;
    }
    if (arg.length() > 0) {
    s =o.m();
    } else {
    System.out.println("zero");
    }
    return s;


    }
    }

    class A extends M {
    public String m() {
    System.setOut(ps);
    System.out.println("a");
    return os.toString();

    }
    }

    class B extends A {
    public String m() {
    System.setOut(ps);
    System.out.println("b");
    return os.toString();
    }
    }




    TESTM Class
    package jUnit.code;
    import org.junit.*;
    import junit.framework.JUnit4TestAdapter;
    import static org.junit.Assert.assertEquals;



    public class testM {

    M test = new M();

    @Before public void setUp() {
    test = new M();
    }
    @After public void tearDown() {
    test = null;
    }
    @Test public void Testm () {
    String arg1 = "Test";
    int i1 = 4;
    String actRes= test.m(arg1, i1);
    String expRes = "b";
    assertEquals(expRes, actRes);
    }
    public static junit.framework.Test suite() {
    return new JUnit4TestAdapter(testM.class);
    }
    }

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    I didn't read all your code (too much of it), but if you System.out.println("b") note that a String "b\r\n" ends up in the byte buffer (the last two chars represent the new line sequence on a MS Window machine). Testing for equality with a String "b" fails of course.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    ok so whats the way out ? how do I cmpare string "b" with baos.toString() ?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,531
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Venny View Post
    ok so whats the way out ? how do I cmpare string "b" with baos.toString() ?
    The output produced is "b\r\n" so you should test agains that String. Not against "b".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Venny is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Redirecting System.err on two output
    By ZioBafio in forum Advanced Java
    Replies: 2
    Last Post: 12-26-2010, 11:19 AM
  2. Save the output from system.out.println into a file
    By Iskatel in forum Advanced Java
    Replies: 5
    Last Post: 10-05-2010, 02:52 PM
  3. Replies: 0
    Last Post: 09-17-2010, 09:48 AM
  4. How to capture System Information
    By chyrl in forum Advanced Java
    Replies: 4
    Last Post: 07-02-2010, 01:26 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •