Discussion:
How do I use Junit to test whether catch the Exception
(too old to reply)
RC
2007-09-13 18:01:18 UTC
Permalink
For example

public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}

I want to use Junit to test myMethod whether
it catches the Exception or not

In Junit I only know

assertTrue
assertEquals
assertNotNull

is there method called assertCatch ?

Thanks!
Joe Attardi
2007-09-13 18:42:02 UTC
Permalink
Post by RC
For example
public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
is there method called assertCatch ?
Try something like:

public void testSomething() {
try {
myMethod();
} catch (IOException ioe) {
fail("myMethod() did not catch the IOException");
}
}

I'm not too familiar with JUnit (I'm a TestNG guy), but I think that
will do it...
--
Joe Attardi | Massachusetts, USA
***@gmail.com | http://thinksincode.blogspot.com/
unknown
2007-09-13 19:21:48 UTC
Permalink
Post by Joe Attardi
Post by RC
For example
public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
is there method called assertCatch ?
public void testSomething() {
try {
myMethod();
} catch (IOException ioe) {
fail("myMethod() did not catch the IOException");
}
}
I'm not too familiar with JUnit (I'm a TestNG guy), but I think that =
will do it...
I think the OP wants to do it the other way round, i.e. fail if the =

exception is not thrown. You'd probably have to set a flag in the catch=
=

block and check it in a finally block, as Manish suggested.

TestNG makes this easy type of check easy:

@Test(expectedExceptions =3D IOException.class)
public void myTest()
{
myMethod();
}

Dan.



-- =

Daniel Dyer
http//www.uncommons.org
Joe Attardi
2007-09-13 19:31:12 UTC
Permalink
Post by unknown
I think the OP wants to do it the other way round, i.e. fail if the
exception is not thrown. You'd probably have to set a flag in the catch
block and check it in a finally block, as Manish suggested.
Or you could have an empty catch block, and just add a call to fail()
right after the method call:

try {
myMethod();
fail("IOException was not thrown!");
} catch (IOException ioe) {
// This is expected!
}

If the exception doesn't get thrown, execution continues to the next
line, which is the call to fail(). If it does get thrown, which we want,
that fail() will be skipped since we jump to the catch block.
Post by unknown
@Test(expectedExceptions = IOException.class)
public void myTest()
{
myMethod();
}
Yup. I love TestNG.
Although, I think I remember reading that the latest and greatest JUnit
has added an annotation to expect an exception... but I'm not sure at
the moment.
--
Joe Attardi | Massachusetts, USA
***@gmail.com | http://thinksincode.blogspot.com/
Manish Pandit
2007-09-13 18:58:40 UTC
Permalink
Post by RC
For example
public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
is there method called assertCatch ?
Thanks!
I have not tried this, but you can set a flag in catch{}, and assert
on it in finally{}.

-cheers,
Manish
Thomas Fritsch
2007-09-13 21:12:46 UTC
Permalink
Post by RC
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
is there method called assertCatch ?
JUnit has a class ExceptionTestCase for this. See
http://junit.sourceforge.net/javadoc/junit/extensions/ExceptionTestCase.html
--
Thomas
voorth
2007-09-24 13:05:05 UTC
Permalink
Post by RC
For example
public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
Have a good look at the JUnit documentation - there are lots more!
BTW, are you using version 3.x or 4.x?
Post by RC
is there method called assertCatch ?
Thanks!
The trick here is to have two tests:

JUnit 4.0 and up:
@Test public void testNormalFlow() {
myMethod();
// put your assertions here;
}

@Test(expected=java.io.IOException.class)
public void testFailure() {
// set up failure condition
myMethod;
}

JUnit 3.8 has a different way to test for exceptions:

public void testFailure() {
// setup failure condition
try {
myMethod();
fail("myMethod should have thrown an IOException");
}
catch (IOException e) {
// do additional asserts
}
}
proudbug
2007-09-26 03:00:48 UTC
Permalink
I did something like:

public void testFailure() {
// setup failure condition
try {
// some code
fail("XXXX method failed to throw a ZZZZException");
}
catch (ZZZZException e) {
// some code
assertTrue(True);
}

-x
Post by RC
For example
public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
is there method called assertCatch ?
Thanks!
proudbug
2007-09-27 02:23:16 UTC
Permalink
For JUnit 4.X, you can also use new annotation feature:

@Test(expected= XXXXException.class) public void myMethod() {
// code to be tested
}

if this XXXXException is expected to be thrown. -x
Post by voorth
public void testFailure() {
// setup failure condition
try {
// some code
fail("XXXX method failed to throw a ZZZZException");
}
catch (ZZZZException e) {
// some code
assertTrue(True);
}
-x
Post by RC
For example
public void myMethod() {
try {
// do something;
} catch (IOException ioe) {
System.err.println(ioe);
}
}
I want to use Junit to test myMethod whether
it catches the Exception or not
In Junit I only know
assertTrue
assertEquals
assertNotNull
is there method called assertCatch ?
Thanks!- Hide quoted text -
- Show quoted text -
Felipe
2007-10-08 19:22:59 UTC
Permalink
Hi, all!

Could anyone tell me why the following test fails if run by Eclipse
v3.3?

Here's the class:
public class ProvaJUnit {
public void yuppidu () throws IllegalArgumentException {
throw new IllegalArgumentException ();
}
}

Test case follows:
import org.junit.Test;
import junit.framework.TestCase;
public class ProvaJUnitTest extends TestCase {
@Test(expected=IllegalArgumentException.class)
public void testYuppidu () {
new ProvaJUnit ().yuppidu();
}
}

Classes are in the same package. The test fails but the failure trace
is as follows:
java.lang.IllegalArgumentException
at provajunit.ProvaJUnit.yuppidu(ProvaJUnit.java:13)
at provajunit.ProvaJUnitTest.testYuppidu(ProvaJUnitTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
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.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:
130)
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)

Shouldn't the test succeed?
Thanks.
Arne Vajhøj
2007-10-09 00:23:13 UTC
Permalink
Post by Felipe
Could anyone tell me why the following test fails if run by Eclipse
v3.3?
public class ProvaJUnit {
public void yuppidu () throws IllegalArgumentException {
throw new IllegalArgumentException ();
}
}
import org.junit.Test;
import junit.framework.TestCase;
public class ProvaJUnitTest extends TestCase {
@Test(expected=IllegalArgumentException.class)
public void testYuppidu () {
new ProvaJUnit ().yuppidu();
}
}
The use of TestCase makes JUnit think it is version 3 tests.

Try:

import org.junit.*;

public class ProvaJUnitTest {
@Test(expected=IllegalArgumentException.class)
public void testYuppidu () {
new ProvaJUnit ().yuppidu();
}
}


Arne
Felipe
2007-10-09 07:11:44 UTC
Permalink
Thank you! It works.

Continue reading on narkive:
Search results for 'How do I use Junit to test whether catch the Exception' (Questions and Answers)
10
replies
Java Vs. C#?
started 2007-01-04 01:45:54 UTC
programming & design
Loading...