This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Testing and the fly likes Tough Time Getting @Test(exception To Work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Tough Time Getting @Test(exception To Work" Watch "Tough Time Getting @Test(exception To Work" New topic
Author

Tough Time Getting @Test(exception To Work

Bob Nedwor
hangman
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

Thanks for any help with this. What is the trick to getting the @Test(exception syntax to work correctly?
If I don't prefix the method name with "test", it gives me: No Tests Found in ..."
If I do prefix the method name with "test", it fails with an exception even though I am testing to ensure that I do get the exception. I am doing something wrong, but I don't know what it is




Bob N
SCJP - 1.4
SCJD - (B&S) Used 1.5 And It Runs On Solaris10
SCWCD - Thanks to HFSJ!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure if it'll run without the main class shown in the FAQ; I don't recall off the top of my head though since I always run them via IDE or Ant or Maven.
Bob Nedwor
hangman
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

org.junit.runner.JUnitCore has a main class. If I change the method name so that it is prefixed with "test" (see below), it runs, but this one passes rather than fails, even though the exception is not thrown. I thought the purpose of the "exception=" parameter with the @Test tag is so that the test will pass when an expected exception is thrown and fail when it is not.


nedwor /home/nedwor/projects/temp>cat Tester.java

nedwor /home/nedwor/projects/temp>javac -cp junit-4.8.1.jar Tester.java
nedwor /home/nedwor/projects/temp>java -cp junit-4.8.1.jar:. org.junit.runner.JUnitCore Tester
JUnit version 4.8.1
.
Time: 0.007

OK (1 test)

nedwor /home/nedwor/projects/temp>
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The main class in the class under test.

http://junit.sourceforge.net/doc/faq/faq.htm#tests_1
Bob Nedwor
hangman
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

I see the problem now. I should not have been extending TestCase. Now @Test(exception=.. works like it should.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Ah; cool--good to know. I don't think I've run a "bare" test in a half-decade--is there any reason you're doing it like that, or just learning jUnit?
Bob Nedwor
hangman
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

Thanks.
I always try to get something to work in the "bare" fashion, when I can't get it to work in the IDE - to eliminate hidden env settings, etc. I do not have too much experience with JUnit. This is the first time I have used the @Test tag and the first time with the (exception= parameter. Previously I was using the try/catch block way of ensuring that an exception was thrown when it should have been.

The bummer is that now, without extending Testcase, I can't use super.setUp() and super.TearDown() . I am able to just put the "setUp" stuff in a constructor. Thanks for a suggestion as to how to deal with that part.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Use the @Before and @After annotations?
Bob Nedwor
hangman
Ranch Hand

Joined: Aug 17, 2005
Posts: 215

Those work perfectly!
Thanks so much, David! You're a real Javaranch winner!
 
 
subject: Tough Time Getting @Test(exception To Work
 
Similar Threads
NoClassDefFoundError - from command line
Junit error
GWTTestCase error -- The test class ... was not found in module ...
Can't run JUnit 4 test in Eclipse
struts test case problem