1

I need to write test cases for VoltDb procedures using mockito framework . The database returns as array of tables from which I need to get the table using index number and then iterate rows to get values from columns . So if I could get a sample on mocking voltdb connection, table and result it would be great.

Client voltDbClient;

@Mock
DAOFactory daoFactory;

@Test
public void testGetECMPWaitOverSelect() throws Exception {
    String procedure = "Procedure Name is here";
    int offset = PropertiesLoader.getIntValue(PropertiesLoader.OFFSET);
    int fetchlimit = PropertiesLoader.getIntValue(PropertiesLoader.WAITOVER_FETCH_LIMIT);
    Mockito.when(voltDbClient.callProcedure(procedure, offset,fetchlimit)).thenReturn(voltDbClient.callProcedure(procedure, offset, fetchlimit));

    VoltTable[] voltTable = voltDbClient.callProcedure(procedure, offset, fetchlimit).getResults();     

    Mockito.verify(voltDbClient,Mockito.times(1)).callProcedure(procedure, offset, fetchlimit);
}

I expected the table to get mocked results as the class is mocked and verify db procedure is executed or no but since it is not the right way to do it, i did not get appropriate results.

second
  • 4,069
  • 2
  • 9
  • 24
  • What is the class you want to test here? It looks like it could be `voltDbClient` but then you mock it. You should not mock the class under test. Also the line with `voltDbClient.callProcedure` doesn't make sense. You might want to add a code example of the class under test, its dependencies and constructors. – second Aug 22 '19 at 12:04
  • `voltDbClient.callProcedure`. This calls a procedure where the query is already assigned for it and gets the result. – Biswajit Kumar Aug 23 '19 at 04:01
  • But you have mocked `voltDbClient`, so it will not test your real implementation. (if it is not mocked you can not pass into Mockito's `when` or `verify` method). Also `Mockito.when(voltDbClient.callProcedure(procedure, offset,fetchlimit)).thenReturn(voltDbClient.callProcedure(procedure, offset, fetchlimit));` in the best case it returns null, in the worst case you have created an endless loop. But actually mockito should complain about it telling you: "you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed" – second Aug 23 '19 at 09:18

1 Answers1

1

In your when(funcA()).thenReturn(funcA()) you appear to be returning the same function call.

You probably need something more like:-

        when(client.callProcedure("FOO.select", id)).thenReturn(new ClientResponse() {
        @Override
        public byte getStatus() {
            return ClientResponse.SUCCESS;
        }

        @Override
        public byte getAppStatus() {
            return 0;
        }

        @Override
        public VoltTable[] getResults() {
            VoltTable statusRow = new VoltTable(
                new VoltTable.ColumnInfo("StatusId", VoltType.INTEGER),
                new VoltTable.ColumnInfo("LAST_UPDATED", VoltType.TIMESTAMP),
                new VoltTable.ColumnInfo("VAL", VoltType.STRING)
            );
            statusRow.addRow(1, new TimestampType(new Date()), "Hello again");
            return new VoltTable[]{statusRow};
        }

        @Override
        public String getStatusString() {
            return null;
        }

        @Override
        public String getAppStatusString() {
            return null;
        }

        @Override
        public int getClusterRoundtrip() {
            return 0;
        }

        @Override
        public int getClientRoundtrip() {
            return 0;
        }

        @Override
        public long getClientRoundtripNanos() {
            return 0;
        }
    });
    response = client.callProcedure("FOO.select", id);
    VoltTable t = response.getResults()[0];
    assertEquals(t.getRowCount(), 1);
    t.advanceRow();
    long lastUpdatedMicros = t.getTimestampAsLong("LAST_UPDATED");
    long initialDateMicros = initialDate.getTime() * 1000;
    assertTrue(lastUpdatedMicros > initialDateMicros);
    String latestVal = t.getString("VAL");
    assertEquals(latestVal, val);

Sorry it's a very late response, but maybe this might help you or someone else, as stated in the comments, does beg the question what are you attempting to test.

MrChick
  • 120
  • 8