File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit 4 and @RunAs annotation

 
dave taubler
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic