This content has been marked as final. Show 5 replies
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.Oh, brother.
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.Um, no.
Is there something that could be preventing me from writing to file in JUnit TestCase? Could this be a Singleton Issue?
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?
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.
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,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.