August 25, 2004TestSetup and evil static methodsTestSetup is a JUnit class you can use if you want to introduce initialization in your JUnit tests that only get invoked once, and not once before test method, which is what JUnit does (making it difficult to make sure that an initialization method is only invoked once during the lifetime of the class). I wrote more about this oddity not long ago. A recent weblog entry about TestSetup shows how to use it, which is pretty straightforward. I still have a big problem with this solution, and what JUnit forces you to do to work around this "design choice": it forces you to introduce static methods in your code. I don't have anything against static methods in general, I tend to use them as much as possible whenever they don't need access to any non-static fields since it decreases the coupling between my classes. However, static methods are particularly evil when they modify state, and by extension, force you to declare static fields. Which is exactly the only choice that JUnit leaves you. The problem with non-const static methods is that they break badly in several cases:
Classes in object-oriented programming obey very simple rules:
Of course, these is also an alternative. :-) Comments
I'd love to use TestNG, unfortunately it's JDK 5 only. I'll stick with static initialization blocks for now. Posted by: Matt Raible at August 25, 2004 02:53 PMUm, I can't see what you're getting at, Cedric. I know you and I have disagreed on JUnit before, but: The whole idea of TestSetup is to give you a method that is only invoked once per Test, where TestCase and TestSuite are instances of Test. Even if you use the convenience static suite() method to create the TestSuite, the setUp and tearDown is only called once per run of the TestSuite. Isn't that what you're asking for? Posted by: Robert Watkins at August 25, 2004 04:15 PMIt might sound stupid but I am trying to learn here. YOu mentioned that the reuse of the same JVM will cause problem since the class is using static variable. Could you please elaborate on that? Why would the code break? Posted by: Roger Rustin at August 25, 2004 06:27 PMIt might sound stupid but I am trying to learn here. YOu mentioned that the reuse of the same JVM will cause problem since the class is using static variable. Could you please elaborate on that? Why would the code break? Posted by: Roger Rustin at August 25, 2004 06:28 PMJUnit is not forcing you to use static methods at all. In the example quoted above, you could simply move the start method into the TestSetup class. Posted by: Ilja Preuß at August 27, 2004 01:23 AMYou say: How does this decrease coupling between classes? Ben I may be missing something, but what's to stop you from creating a Singleton to hold your "static" initialization stuff? There may be multiple calls to "setup", but of course the Singleton will only be initialized once. Posted by: robspassky at November 23, 2005 06:42 AMBen, it decreases coupling (kinda) because classes don't have to store references to each other if they are only using static methods. I'm not sure if that is exactly what I'd term "Coupling", since you are still tied to the static methods' signature but I think that's what he was getting at. I suppose what he might have actually been refering to is the fact that if you don't have a reference to the class and need one, you might have to start looking for methods in classes you do have that supply that object, and using that reference would couple you closer to the intermediate class. Oh, and Rob, I recommend not doing the singleton thing. Although it looks promising, you are making an assumption that you will never need two of what that singleton is supplying. That always seems to sound good, but for me seems to fail later. For instance, number of monitors, number of keyboards, a ram cache for a DB (found that I needed to flush one set of data and not another) A "Main" window in my java program, ... I'm not saying it's not tempting and often useful, but I'd have saved myself a lot of time (in many cases) if I had just created an instance and passed it around rather than using a singleton, because that's a bitch of a refactor. Posted by: Bill Kress at December 2, 2005 09:19 AMHi all! I've just downloaded Test NG on my system - vista OS, but I cannot locate the executable file to run the application, albeit I've extracted the folders from the zip file. What I'm I doing wrong? Posted by: Rashid at November 7, 2008 09:15 AMPost a comment
|