• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write a unit test to test if the output is printing to console or log file

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a java class to which I need to pass 3 arguments: name, city and log file name. If the name of the log file is "debug", then the output prints to console, else the output redirects to a log file whose name is the third argument. I want to write a unit test to test this functionality,

if args[2] == debug --> pass if the output is printing to console
else --> pass if a file is created with the argument name

Example.java

ExampleTest.java
 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checking if output went to the log file, is as simple as opening and reading the log file afterwards. You will of course want to make sure the file is deleted (or that you have a guaranteed unique file) before you do that, to ensure you are not looking at stale results.

As for the "is it going to the console" part, there is a "java.lang.System.setOut()" call you can look at. This is described in this link http://docs.oracle.com/javase/7/docs/api/java/lang/System.html and it is suggested in this Stack Overflow link http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println . You could test that basically the same way.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All you recent questions are looking pretty similar. Do you know you're essentially asking the same question each time?

Anyway, the solutions I gave you in answer to a previous question of yours (in both Java7 and Java8) will help you test the code you have now. Consider that it's the same problem of being able to sense the interaction with static methods from your test class, in this case System.out and System.err. So review again my previous answers to your other post and you will be able to achieve what you want with a little careful thought and work.

I also mentioned in this also quite similar question thread that using System.setOut() and System.setErr() is a terrible idea as it causes global changes to the System object. It's worse this time as you're doing it in production code and not just test code.
 
L Foster
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my part, I should have read @swathi's post more carefully. I overlooked the use of setOut (which I even suggested). This is test code, no? Either way, excellent point, @Tim about System.setOut() having lingering effects. It also could be problematic if this test is being run in a multi-threaded way. You could easily corrupt the output you intend to check for this test case, other test cases, and worse.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not realize I was asking the same question. Thank you for pointing it out @Tim. I removed lot of my original code, to avoid confusions. My class is called from a shell script, so I need System.exit statements and for the setOut and setErr I am resetting the output back to console in my class.



Coming to my question, I don't have an expected output to use assert statement and check. The output gets generated dynamically, because it depends on lot of other classes and projects as well. So I just wanted to test if the output is writing to console when debug is passed, to log file when an argument other than debug is passed Thank you.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic