Capture output in System.out

Discussion in 'Java' started by Venny, Jan 27, 2011.

  1. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I am trying to write a code that would capture the output from System.out.println and then us this output to run a junit method assertEquals. basically theres one method that needs to be tested and output of that method is in form of system.out("a") and i want to compare "a" with excpected output.

    Can anyone help me how shall i capture the output?

    thanks
    Venny
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved to C# forum
     
  3. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    its a java question.. so shud be moved to Java forums
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved. I only saw System.out and moved to C#. Thanks for pointing that out.
     
  5. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Do you know the answer btw ?
     
  6. alpha34

    alpha34 New Member

    Joined:
    Dec 2, 2009
    Messages:
    25
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    student
    Location:
    somewhere in india
    redirect output to a file and then use that file and a file pointer which can be used to compare or use getxxx() method where xxx means in or out to get output and store it in a string variable use it to compare....
     
  7. alpha34

    alpha34 New Member

    Joined:
    Dec 2, 2009
    Messages:
    25
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    student
    Location:
    somewhere in india
  8. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    What I did is I redirected the output to PrintStream ps and then used method Sytem.setOut(ps); and then convereted ps to String..

    PrintStream ps;
    System.out.println("a");
    System.setOut(ps);
    ps.toString();

    but unfortuantely ps.toString() is not giving me "a" as output..

    Can you explain me where did i went wrong ?
     
  9. alpha34

    alpha34 New Member

    Joined:
    Dec 2, 2009
    Messages:
    25
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    student
    Location:
    somewhere in india
    printstream is output stream object it should be directly used with println() or print() and u r trying to set a object which is a stream "System.setOut(ps);" before doing tht casting of ps object is to be done cast ps to string by using toString() and then set output....code will be same but some changes
    PrintStream ps;
    System.out.println("a");
    ps.toString();
    System.setOut(ps);

    try this....it might work but im bit unsure of use of printstream as i said its a output object and it has it's own syntax..
     
  10. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Thankyou for the response... but it seems the code is not working yet..
    i am trying to capture output "a" to a string which i can later compare to another string and run my junit test case..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice