Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have written something for this same case. You can mock the resultset using Mockito. You can as well loop over the mock rows of resultset by mocking the resultset.next() with this piece of code.</p> <pre><code>// two dimensional array mocking the rows of database. String[][] result = { { "column1", "column2" }, { "column1", "column2" } }; @InjectMocks @Spy private TestableClass testableClass; @Mock private Connection connection; @Mock private Statement statement; @Mock private ResultSet resultSet; @BeforeTest public void beforeTest() { MockitoAnnotations.initMocks(this); } @BeforeMethod public void beforeMethod() throws SQLException { doAnswer(new Answer&lt;Connection&gt;() { public Connection answer(InvocationOnMock invocation) throws Throwable { return connection; } }).when(testableClass).getConnection(); when(connection.createStatement()).thenReturn(statement); when(statement.executeQuery(anyString())).thenReturn(resultSet); final AtomicInteger idx = new AtomicInteger(0); final MockRow row = new MockRow(); doAnswer(new Answer&lt;Boolean&gt;() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { int index = idx.getAndIncrement(); if (result.length &lt;= index) { return false; } String[] current = result[index]; row.setCurrentRowData(current); return true; } ; }).when(resultSet).next(); doAnswer(new Answer&lt;String&gt;() { @Override public String answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); int idx = ((Integer) args[0]).intValue(); return row.getColumn(idx); } ; }).when(resultSet).getString(anyInt()); } static class MockRow { String[] rowData; public void setCurrentRowData(String[] rowData) { this.rowData = rowData; } public String getColumn(int idx) { return rowData[idx - 1]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload