New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@BeforeClass requiring the method to be static is nonsense #122
Comments
other workarounds are:
|
i found a nice workaround which works for every environment. in java there is something like class initializers which are written like static initializers, just without the static. :D See: http://stackoverflow.com/questions/1052577/why-must-junits-fixturesetup-be-static |
the class initializer aint a workaround because it acts just like a @before, because for every test method, a new test object gets initialized... why is that so? |
@BeforeClass must be static because, by design, there is no instance of the class to invoke the @BeforeClass method on. There is an instance of the class at the time of every method invocation. This is a deeply assumed part of the design of several JUnit classes. That said, I'd like to help solve your problem. Can you rephrase the issue as a positive request? "I'd like a way to..." |
I'd like a way to have a method annotated, which is only run once on the first test instance before the first test method is run. (Something between @BeforeClass and @before) I've attached my abstract test class which allows me to do that by remembering the last test instance statically and using the @before and @afterclass annotations to invoke my setUpOnce and tearDownOnce methods which can be overriden in tests like it was done in JUnit3 times (going against the usage of JUnit 4 annotations). |
I am doing various things with the ITestLifecycleHooks, like resetting the database, reconfiguring the spring context beans or overriding mocks via Mockito in the setUpOnce() and tearDownOnce() methods (all requiring Spring-Dependecy-Injection to occur before being run). A neat solution in JUnit would surely be valued by many people who have the same requirements. Thanks for your help. :) |
Got it. This will be much easier if we can get something like https://github.com/KentBeck/junit/issues/199 done, allowing a single rule to work at both the class-wide level and each-method level. |
This issue was closed 2 years ago,but it's still not be resolved,can we reopen it? |
I can reopen for more discussion. I think there's a base problem that we should solve, that I don't understand yet. |
Hi, Thanks! |
I'm tempted to close this. The top-voted answer at http://stackoverflow.com/questions/1052577/why-must-junits-fixturesetup-be-static explains in detail why we can't allow It does sound like there is a desire to have a single rule that can work at both the class-wide level and each-method level. If that would solve the core problem, then we should track that in a bug that states that as the goal. |
It's so frustrating to write an abstract unit test class to test DB calls with JUnit b/c of this. If my abstract class only has one implementer this works fine, but as soon as we have 2 or more and they get run in parallel, we start having state issues because of the static state variables we have to have (either that or we have to create new databases for each test which is kind of overkill and slows test execution time down). I miss JUnit 3 and TestNG -- these are a better fit for these kinds of tests since it appears JUnit 4 will never be fixed to support this kind of test (abstract test classes that need to initialize state once for all tests in the implementing sub-class). |
@dougschroeder How did you do per-test-class initialization in JUnit3? Instead of having a common base class for database tests, have you considered using |
Hi @kcooney, thank you for the quick response! Actually, it looks like our JUnit 3 tests were not doing one-time initialization in non-static methods either. I was able to get a solution working in JUnit 4 that allows me to do class-level setup and teardown in non-static methods by extending BlockJUnit4ClassRunner and having my base test class extend an interface that allows it to setup and tear down via an instance method. I used this blog article as a guide: http://saltnlight5.blogspot.com/2012/09/enhancing-spring-test-framework-with.html I didn't know about |
@dougschroeder while the solution on the blog post would allow a one-time setup to.happen in a non-static method, unless I am missing something, the one-time setup would have to store state in a static field if you want the data available in more that one test. For the use case you give, you use need 1) a class extending |
@kcooney can I use that without having to group my implementations into a Suite? I'd like to define my base test class in a common jar and not have to know about the tests that extend my base class. To work around having to store my state in a static field, I had the
Not the most elegant solution, so I'd rather use |
@dougschroeder you don't need to define a suite. |
Using the Rules annotation is very easy to use and solved the problem for me. |
Just use testNg - does not require methods annotated @BeforeClass to be static - problem solved the way it should be |
Hi,
JUnit is the only testing Framework which requires @BeforeClass Annotated methods to be static. This makes @BeforeClass mostly useless für Enterprise Applications, because often you have to do a setUpOnce after the dependency injection has been done (e.g. Spring or some other Framework).
As a workaround @before with a boolean field and an initialized check is often used which is against the design of JUnits @before, but is required because @BeforeClass is mutilated by design...
Also see:
http://stackoverflow.com/questions/1052577/why-must-junits-fixturesetup-be-static
Best Regards
subes
The text was updated successfully, but these errors were encountered: