Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #25279
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Reading from stdout |
| Date | 2013-09-12 22:12 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <l0u6s6$s4b$1@dont-email.me> (permalink) |
| References | <l0sruc$clf$1@dont-email.me> |
On 9/12/2013 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? I've run into this problem before as well, and I concluded the same as Daniel: All output should go to a plain vanilla OutputStream (or a Writer). Then you just wrap where you really want the output to go in a plain stream of some sort, and you're set. Anything else, and you're pretty much fooked. I don't have any good suggestions for you, I'm sorry. Other than refactor the whole thing to use regular logging logging instead. So, why aren't use using the regular logging package? Because there's standard loggers, StreamHandler or MemoryHandler, that would do what you want pretty easily, allowing you to inject a stream to intercept and test logging output.
Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread
Re: Reading from stdout markspace <markspace@nospam.nospam> - 2013-09-12 22:12 -0700
csiph-web