How do I use Junit to test whether catch the Exception

R

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!
 
J

Joe Attardi

RC said:
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...
 
M

Manish Pandit

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
 
D

Daniel Dyer

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...

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 = IOException.class)
public void myTest()
{
myMethod();
}

Dan.
 
J

Joe Attardi

Daniel said:
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.

TestNG makes this easy type of check easy:

@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.
 
V

voorth

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?
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
}
}
 
P

proudbug

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
 
P

proudbug

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
 
F

Felipe

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Felipe said:
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();
}
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top