gpt4 book ai didi

java - 如何使用注释@Test(expected = PSQLException.class) 在 Junit 4.12 PSQLException 中进行测试

转载 作者:搜寻专家 更新时间:2023-11-01 03:32:50 26 4
gpt4 key购买 nike

我在测试中期望 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

怎么了?我有 org.postgresql.util.PSQLException: ERROR: insert or... 在第一行我的 StackTrace 和同时 java.lang.AssertionError: Expected exception: org.postgresql.util。 PSQLException 在 StackTrace 的最后一行。

为什么?我该怎么做?我没有使用 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(); } }

你不应该尝试捕捉这个方法。您正在处理异常并记录它。将方法更改为记录(或不记录)并重新抛出异常。请记住,这将更改方法签名,现在它将抛出异常。

例子:

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(expected = PSQLException.class) 在 Junit 4.12 PSQLException 中进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43989681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com