Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Tough Time Getting @Test(exception To Work

 
hangman
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main class in the class under test.

http://junit.sourceforge.net/doc/faq/faq.htm#tests_1
 
Bob Nedwor
hangman
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem now. I should not have been extending TestCase. Now @Test(exception=.. works like it should.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the @Before and @After annotations?
 
Bob Nedwor
hangman
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those work perfectly!
Thanks so much, David! You're a real Javaranch winner!
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!