Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

JUnit 4.4 expected exception not working?

Thread view: 
dom.k.black@googlemail.com - 24 Mar 2008 20:02 GMT
I am fairly new to JUnit so maybe I did something silly here.

I have the following in a test case:

    @Test (expected=ArrayIndexOutOfBoundsException.class)
    public void testOutOfRangeChannel() {
        throw new ArrayIndexOutOfBoundsException();
    }

When I run the test case I get an error, due to the exception. The
trace is:

java.lang.ArrayIndexOutOfBoundsException
    at
net.sf.xwav.soundrenderer.test.TestSoundBuffer.testOutOfRangeChannel(TestSoundBuffer.java:
81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:
81)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
38)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
673)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
196)

I don't understand why the expection is giving a success result? (of
course teh code above is not the actual test I want to do, just a cut
down example)
dom.k.black@googlemail.com - 24 Mar 2008 23:03 GMT
Of course I meant to say I don't understand why the test is failing!

It is receiving the exception I told it to expect.
Patricia Shanahan - 24 Mar 2008 23:12 GMT
> I am fairly new to JUnit so maybe I did something silly here.
>
[quoted text clipped - 7 lines]
> When I run the test case I get an error, due to the exception. The
> trace is:

Are you sure you are running the right JUnit version?

I tried copying the sample code into one of my own tests, and it ran
successfully, no exception trace. The problem has to be in which JUnit
version you are running how, not in the actual test case.

Patricia
dom.k.black@googlemail.com - 24 Mar 2008 23:34 GMT
> dom.k.bl...@googlemail.com wrote:
> > I am fairly new to JUnit so maybe I did something silly here.
[quoted text clipped - 16 lines]
>
> Patricia

Thanks for confirming this. Maybe I picked up a dodgy beta JUnit
version. I will try a different one.
Owen Jacobson - 25 Mar 2008 00:53 GMT
On Mar 24, 6:34 pm, dom.k.bl...@googlemail.com wrote:

> > dom.k.bl...@googlemail.com wrote:
> > > I am fairly new to JUnit so maybe I did something silly here.
[quoted text clipped - 19 lines]
> Thanks for confirming this. Maybe I picked up a dodgy beta JUnit
> version. I will try a different one.

Note that if your test suite is a class derived from TestCase or
TestSuite, JUnit 4 will run it "as if" under JUnit 3's rules, in which
case any exception is an error, and @Test is ignored entirely.  To use
the JUnit 4 features, your class should be derived from Object -- and
you don't need to name your test cases testSomething, just something,
provided they're correctly annotated.

-o
Patricia Shanahan - 25 Mar 2008 01:06 GMT
> On Mar 24, 6:34 pm, dom.k.bl...@googlemail.com wrote:
>>
[quoted text clipped - 23 lines]
>
> -o

Indeed, the test in which I embedded the sample code is designed as a
JUnit 4 test, and does not extend TestCase.

Patricia
dom.k.black@googlemail.com - 25 Mar 2008 09:01 GMT
> On Mar 24, 6:34 pm, dom.k.bl...@googlemail.com wrote:
>
[quoted text clipped - 30 lines]
>
> -o

Thanks, that might be it.

I changed the class to be derived from TestCase.

But if I don't do that, I can add the test to the AllTests test suite
(addSuite needsa TestCase subclass)?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.