5 Replies Latest reply: Mar 13, 2008 8:29 PM by 796254 RSS

    Writing to file in JUnit test case? Not working?

    807591
      I have a singleton class called InstantLogger that internally uses a PrintWriter to write to file. It is very basic, has startLogger(String filename), log(String msg), and stopLogger(). startLogger just creates file, log write to file, and stopLogger closes file.

      When I use this class outside of the JUnit test suite it works fine.

      As soon as I use it in a JUnit test it does not write to file. It creates the file (this happens in the TestSuite), but will not write anything to it (when log is called from TestCase). I put a System.out in the log function and I see that, but still nothing writing to file. And I will say it again, it does work in non TestCase scenarios so I know it works.

      Is there something that could be preventing me from writing to file in JUnit TestCase? Could this be a Singleton Issue?

      Thanks,
        • 1. Re: Writing to file in JUnit test case? Not working?
          796254
          avalanche333 wrote:
          I have a singleton class called InstantLogger
          Ouch.
          that internally uses a PrintWriter to write to file. It is very basic, has startLogger(String filename), log(String msg), and stopLogger(). startLogger just creates file, log write to file, and stopLogger closes file.

          When I use this class outside of the JUnit test suite it works fine.
          Oh, brother.

          >
          As soon as I use it in a JUnit test it does not write to file. It creates the file (this happens in the TestSuite), but will not write anything to it (when log is called from TestCase). I put a System.out in the log function and I see that, but still nothing writing to file. And I will say it again, it does work in non TestCase scenarios so I know it works.

          Is there something that could be preventing me from writing to file in JUnit TestCase? Could this be a Singleton Issue?
          Um, no.

          Your code is wrong. You're assuming something that isn't correct. You're also making the assumption that just because it "works" in another context that it's right in all contexts. The two aren't the same, be it configuration or something else.

          Singleton? That GoF pattern has been voted off the island. Didn't you hear?

          http://code.google.com/p/google-singleton-detector/

          %
          • 2. Re: Writing to file in JUnit test case? Not working?
            807591
            Thanks for the critisism... but thats not really what I am looking for. I am not assuming it works in all context, I am stating which context it does work and which it does not to give people who want to help an idea of whats happening.
            • 3. Re: Writing to file in JUnit test case? Not working?
              796254
              I am not assuming it works in all context,
              I find that when people cite a feature of their code that "works" in another context, they're implicitly saying "there's nothing wrong with my code, it's something else". When you ask if it has something to do with Singleton, that's where it sounds like you're headed.

              Do you not know about the 1000+1 logging packages that are available to you? There's java.util.logging built right into the JDK, Apache Commons logging and, best of all, Log4J. Who in their right mind would see a reason for writing their own logging class?
              I am stating which context it does work and which it does not to give people who want to help an idea of whats happening.
              Sorry, unless you posted the JUnit Test class I don't think I can guess from the information you posted.

              Does your IDE not have a debugger? Why not fire up the JUnit test in the debugger and step through to see what's called? I'm betting that you assume that something is happening that is not. That's when most surprises pop up.

              %
              • 4. Re: Writing to file in JUnit test case? Not working?
                807591
                I want something that will write to file as tests progress. The Logger class in java jdk did not write until all tests were complete, that is why I decided to write my own. Could you suggest a logger that will write as unit tests run?
                • 5. Re: Writing to file in JUnit test case? Not working?
                  796254
                  avalanche333 wrote:
                  I want something that will write to file as tests progress.
                  OK.
                  The Logger class in java jdk did not write until all tests were complete,
                  Sure about that? Sounds like a "flush" problem.
                  that is why I decided to write my own. Could you suggest a logger that will write as unit tests run?
                  I know that log4j writes continuously to the console. I've used it to write continuously to a WebLogic log. I can't say that I've tried it much with JUnit and file loging, because they usually aren't long-running things for me. The tests either work or they don't. If I don't get 100% green I go in and fix the tests. Not much need for logging then. That's just the way I use it. YMMV.

                  %