This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JUnit 4 and @RunAs annotation

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42135
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic