1

I am trying to mock the getMeasureAggregator() of ResultSetRow object - I don't seem to succeed. I am very new in writing mockito unit test.

I want the BuilSQL.formatMeasuer() goes to case 2: so what I have decided to do was mocking row.getMeasureAggregator.

Here is my BuildSQL class:

public class SQLBuilder { public static String buildSQL(JsonObject requestData, JsonObject queryInfo) throws AcquisitionException { JsonArray jArray = queryInfo.get("columns").getAsJsonArray(); Set<String> columns = new HashSet<>(jArray.size()); for (int i = 0; i < jArray.size(); i++) { columns.add(jArray.get(i).getAsString()); } List<ResultSetRow> selectedRows = new ArrayList<>(); List<ResultSetRow> retrievedRows = null; retrievedRows = MetaDataProvider.executeMetadataRequest(queryInfo, requestData); // this method returns a collection of RetrievedResultSetRow for (ResultSetRow retrievedRow: retrievedRows) { if (//some condition evaluates to true) { selectedRows.add(retrievedRow); } } String sql = ""; String select = "SELECT "; for (int i = 0; i < selectedRows.size(); i++) { ResultSetRow row = selectedRows.get(i); select += formatMeasure(row.getMeatureName(), row.getMeasureAggregator()); } select = select.substring(0, select.length() - 1); return sql; } private static String formatMeasure(String measureName, int measureAggregator) { switch(measureAggregator) { case 1: return "sum(\"" + measureName + "\")" + " AS \"" + measureName + "\","; case 2: return "COUNT(\"" + measureName + "\")" + " AS \"" + measureName + "\","; return measureName; } } 

here is my ResultSetRow class:

public class ResultSetRow { private final int iRow; private final int measureAggregator; public ResultSetRow(JsonObject dimensionMetadata) { this.iRow = dimensionMetadata.get("ROW").getAsInt(); this.measureAggregator = dimensionMetadata.get("MEASURE_AGGR").getAsInt(); } public int getMeasureAggregator() { return measureAggregator; } } 

here is how I am mocking

@RunWith(PowerMockRunner.class) @PrepareForTest({ HanaClientRequestUtils.class, RetrievedResultSetRow.class }) public class HanaSQLBuilderTest { private ResultSetRow resultSetRow; private requestData; private queryInfo @Test public void formatMeatureExecuteCase2() throws Exception{ resultSetRow = Mockito.mock(ResultSetRow.class); PowerMockito.when(resultSetRow.getMeasureAggregator()).thenReturn(2); String querySQL = HanaSQLBuilder.buildSQL(requestData, queryInfo); System.out.println(querySQL); } } } 

I am not sure why row.getMeasureAggregator() does not return 2?

4
  • You need to retrieve in MetaDataProvider.executeMetadataRequest(...) method the exact object what You mock in Your test. Otherwise the ResultSetRow instances are not mocked by Mockito. Commented May 6, 2019 at 20:43
  • What You can do is to mock also MetaDataProvider.executeMetadataRequest so it returns a list of your mocked ResultSetRows. But because this method is static, Mockito can have problems mocking it (PowerMock can mock static methods as I know, but please check it). Commented May 6, 2019 at 20:44
  • @zolv: thanks for ur reply. I know we can mock static method using powerMockito. can u refer me to some example? Commented May 6, 2019 at 20:58
  • dzone.com/articles/using-powermock-mock-static Commented May 6, 2019 at 21:00

1 Answer 1

1

Additionally to my comments above (I am coding by hand, forgive me some mistakes). You can try with adding a retrievedRows as a parameter in Your buildSQL method:

public static String buildSQL( JsonObject requestData, JsonObject queryInfo, List<ResultSetRow> retrievedRows) throws AcquisitionException { // ... rest of Your code adopted to new parameter 

}

And then provide Your retrievedRows filled with mocks

@Test public void formatMeatureExecuteCase2() throws Exception{ resultSetRow = Mockito.mock(ResultSetRow.class); PowerMockito.when(resultSetRow.getMeasureAggregator()).thenReturn(2); List<ResultSetRow> retrievedRowsMock = new ArrayList<>(1); retrievedRowsMock.add(resultSetRow); String querySQL = HanaSQLBuilder.buildSQL(requestData, queryInfo, retrievedRowsMock); System.out.println(querySQL); } } 

I hope it will guide You to solution.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.