组织是一个POJO。
这是测试类:
@Tested
DAOImpl daoImpl;
@Injectable
JdbcTemplate mockJdbcTemplate;
@Mocked
DAO ssoDAO;
@Before
public void setup() {
daoImpl = new DAOImpl();
daoImpl.setJdbcTempate(mockJdbcTemplate);
}
@Test
public void testAddOrg() {
final String expectedId = "7c82facc";
final String expectedOrg = "one";
new Expectations() {{
mockJdbcTemplate.queryForObject(DAOImpl.GET__KEY_FOR_ORG_IDS, (DAORowMapper) any, expectedId, expectedKeys);
result = expectedKeys;
}};
Organization actualKey =daoImpl.addOrg(expectedId, expectedKeys);
assertEquals(expectedKeys, actualKey);
}
}
这是被测试的类:使用我要测试的方法:
@Repository(value = "dao")
public class DAOImpl implements DAO {
private JdbcTemplate jdbcTemplate;
static final String GET_KEY_FOR_ORG_IDS = "select keys from table where id=?";
static final String DATASOURCE_BEAN = "dataSource";
@Autowired
public void createTemplate(
@Qualifier(value = DATASOURCE_BEAN) DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public SASOrganization addOrgSASRelationship(String orgIds, String orgKeys) {
try {
Organization key = jdbcTemplate.queryForObject(GET_KEY_FOR_ORG_IDS, new DAORowMapper(), id, keys);
return key;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
public void setJdbcTempate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
题:
我正在获取
java.lang.ClassCastException
。如何解决此特定代码中的问题?
如果您可以提供经过编辑的代码或一个很好的解释。
最佳答案
我将在Expectations块中使用withInstanceOf(DAORowMapper.class)
而不是(DAORowMapper)any
。