简体   繁体   中英

jdbcTemplate Mockito unit testing

I'm trying to perform unit testing and write a test for jdbctemplate.query . I need the code coverage for the below method.

Code:

   ` public List<User> UserInfo(final Long runId){
    jdbcTemplate.setFetchSize(10);
    List<User> userList = jdbcTemplate.query(con -> con.prepareStatement(GET_USER_INFO)
            , (rs, rowNum) -> {
                String userName = rs.getString("username");
                String shoppingItems = rs.getString("shopingItems");
                Long shoppingid = rs.getLong("shoppingid");
                User user= new User();
                user.setCmts(userName);
                user.setNodeid(shoppingItems);
                user.setNodename(shoppingid);
                user.setRunId(runId);
                return user;
            });
    return userList;

}`

I have written the test as below, but running the test "coverage as junit" doesn't show me any code coverage for method which is inside the (rs, rowNum) -> {

Test:

`@InjectMocks
private OracleRepository oracleRepository;

@Mock
private JdbcTemplate jdbcTemplate;

@Mock
private ResultSet rs;

@Mock
private Connection connection;

@Mock
private PreparedStatement stmt;

@Mock
private RowMapper rowMapper;

@Test
public void UserInfoTest() throws SQLException {
    String username= "joe";
    String shoppingItems= "3";
    long shoppingId = 1123456778;
    long runId = 2;
    List<User> listUser = new ArrayList<User>();
    Mockito.when(rs.getString("cmts")).thenReturn(cmts);
    Mockito.when(rs.getString("node")).thenReturn(node);
    Mockito.when(rs.getLong("nodeid")).thenReturn(nodeid);
     User user= new User();
     user.setUserName(username);
     user.setShoppingItems(shoppingItems);
     user.setShoppingId(shoppingId);
     user.setRunId(runId);
     listUser.add(user);
    Mockito.when(connection.prepareStatement(Mockito.any(String.class))).thenReturn(stmt);
    Mockito.when(rowMapper.mapRow(Mockito.any(ResultSet.class), Mockito.any(Integer.class))).thenReturn(user);
    Mockito.when(jdbcTemplate.query(Mockito.anyString(), Mockito.any(RowMapper.class))).thenReturn(listUser);
    List<User> list = oracleRepository.UserInfo(runId);
}`

How do I solve this problem?

At the bottom of your test after the List<User> you can add:

    ArgumentCaptor<RowMapper> argCaptor = ArgumentCaptor.forClass(RowMapper.class);
    Mockito.verify(jdbcTemplate).query(Mockito.any(PreparedStatementCreator.class), argCaptor.capture());

    RowMapper<User> rowMapper = argCaptor.getValue();

    User userResult = rowMapper.mapRow(rs, -1);
    verify(rs).getString("username");
    verify(rs).getString("shopingItems");
    verify(rs).getLong("shoppingid2");

    assertEqual(cmts, userResult.getCmts()); 

This captures the functional interface you defined, then you call it as the action to test it. I checked the verifies and assertion also. However, this is not the easiest to understand if anyone else ever has to maintain this codebase. You might want to consider creating a class that implements the interface and test that directly for readability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM