我正在尝试对 DAO 类中的方法进行单元测试。 Dao 类使用 JDBC。我正在尝试对其进行单元测试,但不使用测试数据库。我必须使用某种数据结构来存储所有信息来测试它。
public class UserProfilesDao extends JdbcDaoSupport {
@Autowired
private MessageSourceAccessor msa;
public long getUserServiceId(long userId, int serviceId) {
String sql = msa.getMessage("sql.select.service_user_id");
Object[] params = new Object[] { userId, serviceId };
int[] types = new int[] { Types.INTEGER, Types.INTEGER };
return getJdbcTemplate().queryForLong(sql, params, types);
}
}
最佳答案
getJdbcTemplate() 似乎是您想要模拟的方法。
在单元测试中,声明 UserProfilesDao 成员,如下所示:
@Spy
private UserProfilesDao classToTest;
@Spy 是一个 mockito 注释。
在单元测试中声明一个设置方法,如下所示:
@Before
public void preTestSetup()
{
MockitoAnnotations.initMocks(this);
doReturn(what ever you want).when(classToTest).queryForLong(
any(xxx.class),
any(yyy.class),
any(zzz.class));
}
其中 xxx、yyy 和 zzz 是 queryForLong 参数类型。
关于java - 使用 jdbc JAVA 对 DAO 类进行 Spring 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34708074/