【发布时间】:2019-07-04 02:30:52
【问题描述】:
我正在为我的应用程序使用带有 jdbcTemplate 的 spring,并且我想测试 DaoImpl 类。有插入、更新和检索操作的实现
道类方法
//dummy class
public class PlayerDAOImpl implements PlayerDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
public Integer getPlayer(int playerId) {
String sql = "SELECT ccount(1) FROM PLAYER WHERE
PLAYER_ID = ?";
return (jdbcTemplate. queryForObject("Query", new Object[]{playerId},
Integer.class)!=0); //here only throws exception
}
//other methods
}
为此,我编写了 Test 类,它成功执行以进行插入和更新,但在检索它时给出了 nullpointer 异常。
@RunWith(MockitoJUnitRunner.class)
class Test{
@InjectMocks
PlayerDAOImpl dao;
@Mock
JdbcTemplate jdbcTemplate;
@Test
public void retrieveResult(){
Mockito.when(dao.getPlayer(int playerId)).thenReturn(false);
//Assert Statement
}}
我已经搜索/尝试过,但没有找到适合我的解决方案。那么如何测试该方法或注入 jdbcTemplate 使其成功。
感谢您的帮助!!
【问题讨论】:
标签: spring mockito junit4 jdbctemplate