如何在Java中使用Mockito模拟结果集并填充它



我有一个代码,用CallableStatement.executeQuery()填充Resultset。我已经模拟了ResultSetCallableStatement,但为了测试该方法,我必须填充ResultSet

这是我测试的方法的代码

ResultSet rset = cs.executeQuery();
while (rset.next()) {
IndexVolatilityImpl tsImpl = new IndexVolatilityImpl();
tsImpl.setTradeDate(rset.getString("trade_date"));
tsImpl.setTradeTime(rset.getString("trade_time"));
tsImpl.setExprDate(rset.getString("expr_date"));
tsImpl.setSymbol(rset.getString("symbol"));
tsImpl.setTradePrice(rset.getDouble("trade_price"));
tsImpl.setContractMonth(rset.getString("contract_month"));
tsImpl.setMilliSecs(rset.getString("trade_time_thou"));
colIndexVolatilityImpl.add(tsImpl);

我已经嘲笑了CallableStatement和ResultSet,因为它们被嘲笑了,我的rset变成了空的。我想填充结果集,并按照以下进行操作

resultSetMock = Mockito.mock(ResultSet.class);
Mockito.when(resultSetMock.getString("trade_date")).thenReturn("03/10/2011");
Mockito.when(resultSetMock.getString("trade_time")).thenReturn("12:24:56");
Mockito.when(resultSetMock.getString("expr_date")).thenReturn("03/19/2011");
Mockito.when(resultSetMock.getString("symbol")).thenReturn("VIX1");
Mockito.when(resultSetMock.getDouble("trade_price")).thenReturn(Double.valueOf("20.96"));
Mockito.when(resultSetMock.getString("contract_month")).thenReturn("1");
Mockito.when(resultSetMock.getString("trade_time_thou")).thenReturn("165");
Mockito.doReturn(resultSetMock).when(callableStatementMock).executeQuery();

rsetnull

您还应该模拟next()方法,使其在第一次调用时返回true,因为mockito默认情况下会返回false

Mockito.when(resultSetMock.next()).thenReturn(true).thenReturn(false);

我已经为同样的情况写了一些东西。您可以使用Mockito模拟结果集。您还可以通过用这段代码模拟resultset.next()来循环结果集的模拟行。

// 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<Connection>() {
        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<Boolean>() {
        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            int index = idx.getAndIncrement();
            if (result.length <= index) {
                return false;
            } 
            String[] current = result[index];
            row.setCurrentRowData(current);
            return true;
        }
        ;
    }).when(resultSet).next();
    doAnswer(new Answer<String>() {
        @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];
    }
}

10年后,当这个问题被问到时,你很有可能会使用带有h2的Spring Boot 2+作为内存中的DB进行测试。如果是这种情况,那么您可以使用org.h2.tools.Csv类从CSV数据创建ResultSet实现:

import org.h2.tools.Csv;
// mock resultset
String csvResults =
    "0001, John Doen" +
    "0002, Bob Smithn" +
    "0003, Alice Doen";
ResultSet rs = new Csv().read(new StringReader(csvResults), new String[] {"id", "name"});

然后以任何方式使用ResultSet。这里有一个Mockito示例,它将它作为RowCallbackHandler提供给JdbcTemplatequery()调用。

JdbcTemplate mockTemplate = mock(JdbcTemplate.class);
doAnswer(ia -> {
  while (rs.next()) {
    ia.getArgument(2, RowCallbackHandler.class).processRow(rs);
  }
  return null;
}).when(mockTemplate).query(any(String.class), any(SqlParameterSource.class), any(RowCallbackHandler.class));
// change the above when() args to match how you are using JdbcTemplate

我重写了@karthik m的答案,使ResultSet模拟程序独立:

通过使用下面的类,我可以很容易地将查询结果导出为csv,并围绕它编写测试。

并不是ResultSet中的所有方法都被嘲笑,因为我不需要它们,但这些方法使用起来应该相当琐碎。

import no.di.common.util.StringUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
/**
 * Creates a Mock of a ResultSet
 */
public class ResultSetMocker {
    private Map<String, Integer> columnNames = new HashMap<>();
    private Object[][] result;
    public ResultSetMocker(String filename) throws IOException {
        loadData(filename);
    }
    private void loadData(String filename) throws IOException {
        List<Object[]> toRet = new ArrayList<>();
        int numberOfParts = 0;
        LineIterator it = FileUtils.lineIterator(new File(filename), "ISO8859-1");
        try {
            String names = it.nextLine();
            String[] name = names.split(";");
            for(int i = 0; i < name.length; i++) {
                columnNames.put(name[i], i + 1);
            }
            while (it.hasNext()) {
                String line = it.nextLine();
                String[] parts = line.split(";");
                numberOfParts = parts.length;
                Object[] result = new Object[parts.length];
                for(int i = 0; i < parts.length; i++) {
                    if(parts[i].equals("(null)"))
                        result[i] = null;
                    else if(StringUtil.isAllNumeric(parts[i]))
                        result[i] = Integer.parseInt(parts[i]);
                    else
                        result[i] = parts[i];
                }
                toRet.add(result);
            }
        } finally {
            it.close();
        }
        result = toRet.toArray(new Object[toRet.size()][numberOfParts]);
    }
    public ResultSet getResultSet() throws SQLException, IOException {
        ResultSet resultSet = mock(ResultSet.class);
        final AtomicInteger idx = new AtomicInteger(0);
        final MockRow row = new MockRow(columnNames);
        doAnswer(new Answer<Boolean>() {
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                int index = idx.getAndIncrement();
                if (result.length > index) {
                    row.setCurrentRowData(result[index]);
                    return true;
                } else
                    return false;
            }
        }).when(resultSet).next();
        doAnswer(new Answer<String>() {
            @Override
            public String answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                int idx = (Integer) args[0];
                return row.getString(idx);
            }
        }).when(resultSet).getString(anyInt());
        doAnswer(new Answer<String>() {
            @Override
            public String answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                String name = (String) args[0];
                return row.getString(name);
            }
        }).when(resultSet).getString(anyString());
        doAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                String name = (String) args[0];
                return row.getObject(name);
            }
        }).when(resultSet).getObject(anyString());
        doAnswer(new Answer<Integer>() {
            @Override
            public Integer answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                String name = (String) args[0];
                return row.getInt(name);
            }
        }).when(resultSet).getInt(anyString());
        return resultSet;
    }
    static class MockRow {
        Object[] rowData;
        private Map<String, Integer> columnNames;
        public MockRow(Map<String, Integer> columnNames) {
            this.columnNames = columnNames;
        }
        public void setCurrentRowData(Object[] rowData) {
            this.rowData = rowData;
        }
        public String getString(int idx) {
            return (String)rowData[idx - 1];
        }
        public String getString(String name) {
            return (String)rowData[columnNames.get(name) - 1];
        }
        public Object getObject(String name) {
            return rowData[columnNames.get(name) - 1];
        }
        public Integer getInt(String name) {
            return (Integer)rowData[columnNames.get(name) - 1];
        }
    }
}

这里的游戏有点晚了,但看起来你在最初的Mock上只需要这个。。。

Mockito.when(resultSetMock.next()).thenReturn(true);

这是必要的,这样映射器——无论你用什么来映射结果集——都会知道结果集中有数据。

相关内容

  • 没有找到相关文章

最新更新