How to mock 2 or multiple prepared statements per connection in jMock?

StackOverflow https://stackoverflow.com/questions/12636318

  •  04-07-2021
  •  | 
  •  

Frage

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

I am getting the following trace:

org.jmock.core.DynamicMockError: mockPreparedStatement: unexpected invocation Invoked: mockPreparedStatement.executeUpdate() Allowed: expected at least once and has been invoked: setString, is void expected once and has been invoked: executeUpdate, returns <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)

I am not sure what am I doing wrong here?

Thanks.

War es hilfreich?

Lösung

Firstly, you seem to be using jmock1, you might consider moving to jmock2.

The failure says that you're calling executeUpdate() when it's not expected. Are you calling it more than once?

Finally, I usually recommend not writing mock tests against JDBC, even though I wrote a paper many years ago showing how to do it. We talk about "only mock types you own" for a number of reasons, one of which is that mock tests assume that the third party implementation doesn't change. For JDBC, I'd recommend writing integration tests that run against a small instance of the real database.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top