0
public void testCreate() throws ApplicationException {
    DutyDrawback drawback = new DutyDrawback();
    drawback.setSerialNumber("TEST123");
    drawback.setSnProcessInd("Y");
    drawback.setMediaNumber("TEST111");
    drawback.setMnProcessInd("Y");
    drawback.setConfirmedInd("Y");
    drawback.setResponseCode("1");
    drawback.setResponseMsg("MSG");
    drawback.setLastChangedUser("USER");
    drawback.setLastChangedDate(new Date(System.currentTimeMillis()));

    mockILogger.expects(atLeastOnce()).method("informationalEvent");
    Logger.setMockLogger((ILogger) mockILogger.proxy());

    // Set up the statement expectations
    Mock mockStatement = mock(PreparedStatement.class);
    mockStatement.expects(atLeastOnce()).method("setString");
    mockStatement.expects(once()).method("executeUpdate").will(returnValue(1));

    Mock mockStatement1 = mock(PreparedStatement.class);
    mockStatement1.expects(atLeastOnce()).method("setString");
    mockStatement1.expects(once()).method("executeUpdate").will(returnValue(1));

    // Set up the connection expectations
    mockConnection.expects(once()).method("setAutoCommit");
    mockConnection.expects(once()).method("commit");
    mockConnection.expects(once()).method("prepareStatement").will(returnValue(mockStatement1.proxy()));
    mockConnection.expects(once()).method("prepareStatement").will(returnValue(mockStatement.proxy()));

    TVSUtils.setMockConnection((Connection) mockConnection.proxy());
    DutyDrawbackDAO drawbackDAO = new DutyDrawbackDAO();
    int count = drawbackDAO.create(drawback);
    assertEquals(2, count);
}

我得到以下跟踪:

org.jmock.core.DynamicMockError:mockPreparedStatement:意外调用被调用:mockPreparedStatement.executeUpdate() 允许:至少预期一次并且已被调用:setString,预期一次无效并且已被调用:executeUpdate,返回 <1>

at org.jmock.core.AbstractDynamicMock.mockInvocation(AbstractDynamicMock.java:96)
at org.jmock.core.CoreMock.invoke(CoreMock.java:39)
at $Proxy2.executeUpdate(Unknown Source)
at com.cat.tvs.dao.DutyDrawbackDAO.create(DutyDrawbackDAO.java:102)
at com.cat.tvs.dao.DutyDrawbackDAOTest.testCreate(DutyDrawbackDAOTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:58)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60)
at java.lang.reflect.Method.invoke(Method.java:391)
at junit.framework.TestCase.runTest(TestCase.java:164)
at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

我不确定我在这里做错了什么?

谢谢。

4

1 回答 1

3

首先,您似乎正在使用 jmock1,您可能会考虑迁移到 jmock2。

失败表明您在不期望的情况下调用了 executeUpdate()。你不止一次调用它吗?

最后,我通常建议不要编写针对 JDBC 的模拟测试,尽管我多年前曾写过一篇论文来展示如何做到这一点。我们谈论“只有你拥有的模拟类型”有很多原因,其中之一是模拟测试假设第三方实现不会改变。对于 JDBC,我建议编写针对真实数据库的小实例运行的集成测试。

于 2012-09-28T09:59:13.150 回答