我在测试中期待PSQLException
:
@Test(expected = org.postgresql.util.PSQLException.class)
public void whenAdditionInProposalWhereAuthorNotExistThen() {
final Proposal proposal = new Proposal();
proposal.setUrlRecruiter("url_recruiter");
proposal.setUlrPropose("url_propose");
proposal.setHeader("header");
proposal.setAuthor("author_which_not_exist_in_recruiter_table");
proposal.setCreate(new Timestamp(System.currentTimeMillis()));
final InjectorInProposal injector = new InjectorInProposal(properties, connection);
//Testing ingection.
injector.injectInProposal(proposal); //(line 115)this throw PSQLException
}
方法:
public void injectInProposal(final Proposal proposal) {
try (final PreparedStatement statement =
connection.prepareStatement(
properties.getValue("add_to_proposal"))
) {
statement.setString(1, proposal.getHeader());
statement.setString(4, proposal.getNickname());
statement.setString(2, proposal.getUlrPropose());
statement.setTimestamp(3, proposal.getCreateTime());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
我的测试失败,但我的StackTrace告诉我:
org.postgresql.util.PSQLException: ERROR: insert or update on table "proposal" violates foreign key constraint "proposal_nickname_fkey"
Подробности: Key (nickname)=(author_which_not_exist_in_recruiter_table) is not present in table "recruiter".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
at ru.pravvich.jdbc.action.InjectorInProposal.injectInProposal(InjectorInProposal.java:48)
at ru.pravvich.jdbc.action.InjectorInProposalTest.whenAdditionInProposalWhereAuthorNotExistThen(InjectorInProposalTest.java:115)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
java.lang.AssertionError: Expected exception: org.postgresql.util.PSQLException
什么错了?我在StackTrace的第一行有org.postgresql.util.PSQLException: ERROR: insert or...
,StackTrace的最后一行有java.lang.AssertionError: Expected exception: org.postgresql.util.PSQLException
。
为什么呢?我怎么能这样做?我不使用ORM系统,只使用干净的JDBC驱动程序。
答案 0 :(得分:1)
问题在于:
public void injectInProposal(final Proposal proposal) { try (final PreparedStatement statement = connection.prepareStatement( properties.getValue("add_to_proposal")) ) { statement.setString(1, proposal.getHeader()); statement.setString(4, proposal.getNickname()); statement.setString(2, proposal.getUlrPropose()); statement.setTimestamp(3, proposal.getCreateTime()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } }
你不应该尝试捕捉这种方法。您正在使用异常并只记录它。将方法更改为记录(或不记录)并重新抛出异常。请记住,这将改变方法签名,现在它将抛出异常。
示例:
public void injectInProposal(final Proposal proposal) throws SQLException { try (final PreparedStatement statement = connection.prepareStatement( properties.getValue("add_to_proposal")) ) { statement.setString(1, proposal.getHeader()); statement.setString(4, proposal.getNickname()); statement.setString(2, proposal.getUlrPropose()); statement.setTimestamp(3, proposal.getCreateTime()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace();
扔掉; }}
您也可以更改@ Test.expected类