How to test in Junit 4.12 PSQLException with @Test annotation (expected = PSQLException.class)

I expected PSQLExceptionin the test:

@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

}

Method:

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

My test failed, but my StackTrace will show me:

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

What's wrong? I have org.postgresql.util.PSQLException: ERROR: insert or...my StackTrace in the first line and the same time java.lang.AssertionError: Expected exception: org.postgresql.util.PSQLExceptionin the last line in StackTrace.

Why? How can i do this? I do not use the ORM system, only a pure JDBC driver.

+4
source share
1 answer

The problem is here:

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

. . log ( ) Exception. , , 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();

throw e; }}

, @Test.expected.

+1

Source: https://habr.com/ru/post/1677208/


All Articles