Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25276 > unrolled thread

Re: Reading from stdout

Started byDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
First post2013-09-12 10:56 -0700
Last post2013-09-12 10:56 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Reading from stdout Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-09-12 10:56 -0700

#25276 — Re: Reading from stdout

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2013-09-12 10:56 -0700
SubjectRe: Reading from stdout
Message-ID<DfnYt.29309$cs5.9201@fx09.iad>
On 9/12/13 9:59 AM, White Spirit wrote:
> I'm writing a unit test for a logger class that writes to stdout.  The
> logger class uses an OutputStreamWriter to wrap FileDescriptor.out:
>
>    mOut = new OutputStreamWriter(new
> FileOutputStream(FileDescriptor.out), "UTF-8");
>
> My unit test instantiates the logger class as a local object.  When I
> try to read from FileDescriptor.out in my unit test class, however,
> nothing is received:
>
>   BufferedReader reader = new BufferedReader( new InputStreamReader( new
> FileInputStream( FileDescriptor.out ) ) );
>   // do some logging
>   // ...
>   String n = reader.readLine();
>
> Is there any way of reading what the logger is outputting?

If you allow the Logger to accept an OutputStream, then you can pass in 
a new ByteArrayOutputStream().

  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final Logger logger = new Logger(baos);
  doSomeLogging(logger);
  String result = baos.toString(); // uses default encoding.
  assertEqual(expectedResult, result);


[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web