The crux of my problem is that the new JUnit 4.4. annotation, @RunAs, seems to have no effects whatsoever. Since in my Googling, I've found plenty of examples of code that uses the annotation, I must be doing something wrong.
I'm using Hibernate 3, Spring 2.5, and JUnit 4.4 What I am attempting to do is to write unit tests for my Hibernate DAOs. Although in production they do not create their own transactional context (rather, a transactional context is created for them) I need them to create their own transactional context while running as unit tests.
To do this, it seems that the best approach is to annotation my unit tests using Spring's @Transactional annotation. In order for that to work, the tests need to run under SpringJUnit4ClassRunner. However, annotating my test class with
has no effect. When I debug the unit test, stop at a breakpoint, and look at the stack trace, I see that the test is always being run with JUnit's RemoteTestRunner.
It may be worth noting that I am running my JUnits within Eclipse (right-click on the class, and choose Run As -> JUnit Test).
I've tried running as other JUnit runners (@RunWith(Suite.class), for example) to see if that would have any effect; it did not. The tests still ran with RemoteTestRunner.
What on earth could I be doing wrong? At this point I'm not even concerned with creating the transactional boundaries; I just want to figure out why I can't tell JUnit what to Run As.
Code is below:
Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>
Dave, What version of Eclipse are you using? I am able to use custom @RunAs in Eclipse 3.3.
Can you select Run--> Open Run Dialog and select your test to confirm "JUnit 4" is selected?
Also, can you confirm this is really a JUnit 4 test? One way to do this is to change "testFindbyEventId" to "davetest" and see if you get an error. If the superclass is TestCase, I think it might be ignoring your test runner.
Joined: May 15, 2001
Ah ha! I did as you suggested (opening the Run dialog and checking the runner) and it indeed was JUnit 3. I'd assumed that since I set my project build path to contain JUnit 4.4 instead of JUnit 3, I'd assumed that the tests would all run as JUnit 4 when I right-clicked and chose Run As -> JUnit Test. Guess I was wrong!
Anyway, thanks a bunch! I ran the test from the Debug dialog and stopped at a breakpoint, and verified that it's running as SpringJUnit4ClassRunner, s oI think I'm good to go.
The problem I am facing is When I right click on test class and select Run As JUnit test, JUnit View does open up but I am not
able to view any test statistics like Runs, Errors, Failures.
Just a text "Launching TestYGUserDao.." on the top-left corner of the JUnit view and ultimately "Terminated" displays.
The log messages on the console are displayed.
One thing I noticed is that when I debug my test class, in the Debug perspective in the JUnit view I can correctly see the test statistics, i.e. the failures if any, the bar getting green on successful completion, etc.
But I am not able to see the results either in Java or Java EE perspectives..
Can anybody please help me by figuring out the problem?
The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU