当我们通过参数(RowMapper)传递时,如何模拟SpringJDBC

当我们通过参数(RowMapper)传递时,如何模拟SpringJDBC,spring,junit,mockito,powermockito,Spring,Junit,Mockito,Powermockito,这是我的课 @Repository public class JdbcRolesDao implements RolesDao{ private JdbcTemplate jdbcTemplate; private static final String GET_USER_ROLES_QUERY = "select ROLE_CD from USER_ROLES where USER_ID = ? AND USER_DRCTRY = ?"; @Autowired public JdbcRol

这是我的课

@Repository
public class JdbcRolesDao implements RolesDao{
private JdbcTemplate jdbcTemplate;

private static final String GET_USER_ROLES_QUERY = "select ROLE_CD from USER_ROLES where USER_ID = ? AND USER_DRCTRY = ?";

@Autowired
public JdbcRolesDao(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public List<String> getRolesByUser(String userId, String directoryId){
    List<String> roles = jdbcTemplate.query(
            GET_USER_ROLES_QUERY , new RoleMapper(), new Object[]{userId, directoryId});
    return roles;
}}
@存储库
公共类JdbcRolesDao实现RolesDao{
私有JdbcTemplate JdbcTemplate;
私有静态最终字符串GET_USER_ROLES_QUERY=“从USER_ROLES中选择角色,其中USER_ID=?和USER_DRCTRY=?”;
@自动连线
公共JdbcRolesDao(数据源数据源){
this.jdbcTemplate=新的jdbcTemplate(数据源);
}
公共列表getRolesByUser(字符串用户ID、字符串目录ID){
列表角色=jdbcTemplate.query(
获取用户角色查询,新角色映射(),新对象[]{userId,directoryId});
返回角色;
}}
这是我的测试课

@RunWith(PowerMockRunner.class)
@PrepareForTest({JdbcRolesDao.class})
public class JdbcRolesDaoTest {

@Mock
private DataSource datasource;

@Mock
private JdbcTemplate jdbcTemplate;

private JdbcRolesDao jdbcRolesDao;

private static AuthenticationResourceTest2 authenticationResourceTest;

private static final String GET_USER_ROLES_QUERY = "select ROLE_CD from USER_ROLES where USER_ID = ? AND USER_DRCTRY = ?";

@BeforeClass
public static void init(){
   authenticationResourceTest = new AuthenticationResourceTest2();
}

@Before
public void initMocks() throws Exception {
   MockitoAnnotations.initMocks(this);
   PowerMockito.whenNew(JdbcTemplate.class).withAnyArguments().thenReturn(jdbcTemplate);
   jdbcRolesDao = new JdbcRolesDao(datasource);
}

@Test
public void getRolesByUserTest(){
    PowerMockito.when(jdbcTemplate.query(anyString(), any(RowMapper.class),any(Object[].class))).thenReturn(authenticationResourceTest.getDummyRoles());
    List<String> configList = jdbcRolesDao.getRolesByUser("UserId", "DirectoryId");
    if(configList != null)
        System.out.println("not null "+configList.size());
    //assertThat(configList, Matchers.hasSize(1));
}}
@RunWith(PowerMockRunner.class)
@PrepareForTest({JdbcRolesDao.class})
公共类JdbcRolesDaoTest{
@嘲弄
私有数据源;
@嘲弄
私有JdbcTemplate JdbcTemplate;
私有JdbcRolesDao JdbcRolesDao;
私有静态AuthenticationResourceTest2 authenticationResourceTest;
私有静态最终字符串GET_USER_ROLES_QUERY=“从USER_ROLES中选择角色,其中USER_ID=?和USER_DRCTRY=?”;
@课前
公共静态void init(){
authenticationResourceTest=新建AuthenticationResourceTest2();
}
@以前
public void initMocks()引发异常{
initMocks(this);
whenNew(JdbcTemplate.class).withAnyArguments().thenReturn(JdbcTemplate);
jdbcRolesDao=新的jdbcRolesDao(数据源);
}
@试验
public void getRolesByUserTest(){
when(jdbcTemplate.query(anyString()、any(RowMapper.class)、any(Object[].class)))。然后返回(authenticationResourceTest.getDummyRoles());
List configList=jdbcRolesDao.getRolesByUser(“UserId”、“DirectoryId”);
if(configList!=null)
System.out.println(“非空”+configList.size());
//资产(configList,Matchers.hasSize(1));
}}
我得到的列表大小为0,但应该是1

当我从源代码和测试代码中删除项目[]{}信息时,它工作得很好

我嘲笑Oject[]信息有点不对劲,
有人可以指导我吗。

您可以使用Matchers.anyObject()而不是any(Object[].class)。(import org.mockito.Matchers;)

我试过这样做,它成功了,PowerMockito.when(jdbcTemplate.query(Matchers.anyString()、Matchers.any(RowMapper.class)、Matchers.anyObject())。然后返回(authenticationResourceTest.getDummyRoles());唯一的区别是我没有PowerMockito.whenNew。我认为它不是必需的,因为它已经有@Mock注释了