| Author |
JUnit 4 and @RunAs annotation
|
dave taubler
Ranch Hand
Joined: May 15, 2001
Posts: 132
|
|
Hi, 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>
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8146
|
|
|
I think you might get good advice in our "Testing" forum where JUnit is often discussed. I'll move this from "Other Open Source Projects" to "Testing" forum.
|
[My Blog] [JavaRanch Journal]
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26193
|
|
|
Moving it along a little more, I'm going to move it to IDEs. The reason being that the test is written correctly and I think it is something in Eclipse.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26193
|
|
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.
|
 |
dave taubler
Ranch Hand
Joined: May 15, 2001
Posts: 132
|
|
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.
|
 |
Jignesh Gohel
Ranch Hand
Joined: Dec 28, 2004
Posts: 276
|
|
Hi All,
I am facing a problem on similar lines as mentioned in the first post of this thread.
Following is my Test Case code:
Can you select Run--> Open Run Dialog and select your test to confirm "JUnit 4" is selected?
I verified the above and found that my test is using the Test Runner as JUnit 4.
The version of Eclipse I am using is
Eclipse Platform
Version: 3.4.1
Build id: M20080911-1700.
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?
|
Regards,
Jignesh
The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU
|
 |
 |
|
|
subject: JUnit 4 and @RunAs annotation
|
|
|