我期望在测试中使用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驱动程序。

最佳答案

问题出在这里:

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

您不应该尝试使用此方法。您正在接受异常并仅记录它。更改方法以记录(或不记录)并重新抛出异常。请记住,这将更改方法签名,现在它将在此处引发Exception。

例:
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();

抛出e; }}

您也许还应该更改@ Test.expected类

关于java - 如何在带有注释@Test的Junit 4.12 PSQLException中进行测试(期望= PSQLException.class),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43989681/

10-09 04:14