javajunitmockitojunit4voltdb

How to write mocks for VoltDb table and VoltDb results using mockito framework?


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.


Solution

  • 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.