Re: JUnit 4 causes an error when expecting exceptions
On Oct 22, 4:01 pm, Lew <l...@lewscanon.com> wrote:
The problem no doubt is in the code that you didn't post. Post an SSCCE
<http://www.physci.org/codes/sscce.html>
and let us take a look.
I'm betting voorth is correct.
--
Lew
Following is the method that is being tested:
public class someClass
{
private String[] valuesForID = null;
public void someClass(String[] values)
{
this.valuesForID = values;
}
public int insertValues(int ID)
{
int count = 0;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@<ip>:<port>:<sid>");
int size = this.valuesForID.length;
PreparedStatement pStmt = conn.prepareStatement("INSERT
INTO TABLEA(COLA, COLB) " +
"VALUES(?, ?)");
for(int i = 0; i < size; i++)
{
pStmt.setInt(1, ID);
pStmt.setString(this.valuesForID[i]);
pStmt.addBatch();
}
int[] c = pStmt.executeBatch();
for(int i = 0; i < c.length; i++)
if(c[i] >= 0)
count++;
}
catch(Exception e)
{
e.printStackTrace();
}
return count;
}
}
The table that I am inserting to is:
TABLEA
(
COLA INTEGER NOT NULL,
COLB VARCHAR2(10 BYTE) NOT NULL
)
COLA is PK and COLB is FK
Now the test case for method insertValues:
@Test(expected=java.sql.BatchUpdateException.class)
public void insertValue()
{
String[] arr = {"VALA", "VALB"};
someClass inst = new someClass(arr);
int count = inst.insertValues(1);
}
Now in this test case, the value "VALA" is not a valid FK and hence a
BatchUpdateException should be thrown and the test should not cause an
error.
When the test is run on the JUnit Test Results Statistics window I get
the following trace:
1 test caused an error.
com.tester.someClass.someClassTest FAILED
unknown caused an ERROR(0.469 s)
Expected exception: java.sql.BatchUpdateException
java.lang.AssertionError
at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:
100)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
79)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
87)
at
org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:
42)
at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
88)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:
51)
at org.junit.internal.runners.JUnit4ClassRunner
$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
42)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:
36)
On the JUnit Test Results Output window I get from the stack trace
printed from my method:
com.tester.someClass insertValues
java.sql.BatchUpdateException: ORA-02291: integrity constraint
(APPDB.TABLEA_R01) violated - parent key not found
at
oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:
498)
at
oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:
12432)
at com.tester.someClass.insertValues(someClass.java:45)
at com.tester.someClassTest.insertValue(someClassTest.java:74)
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:585)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:
100)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
88)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:
51)
at org.junit.internal.runners.JUnit4ClassRunner
$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
42)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:36)
at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:
297)
at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:
672)
at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:
567)