我使用Mockito来模拟queryForObject,但我得到了NPE。看起来我正在设置所需的一切。
long identity = getJdbcTemplate().queryForObject( getStatementConfiguration().getStatement( KeyConstants.CLIENT_USER_VENDOR_IDENTITY), Long.class);
是我获得NPE的地方。
MUT
public IClientUserVendorDto insert(final IClientUserVendorDto dto) throws DaoException {
try {
String sql1 = getStatementConfiguration().getStatement(KeyConstants.CLIENT_USER_VENDOR_RETRIEVE);
Object args[] = new Object[] {
dto.getClientUserId(),
dto.getVendor().getId(),
dto.getIdentifier(),
Boolean.TRUE,
dto.getCreatedBy(),
dto.getLastModifiedBy()};
//insert
String sql = getStatementConfiguration().getStatement(KeyConstants.CLIENT_USER_VENDOR_INSERT);
getJdbcTemplate().update(sql, args);
//Retrieve
long identity = getJdbcTemplate().queryForObject( getStatementConfiguration().getStatement( KeyConstants.CLIENT_USER_VENDOR_IDENTITY), Long.class);
return (ClientUserVendorDto) getJdbcTemplate().queryForObject(sql1, new Object[] {identity}, new ClientUserVendorRowMapper());
} catch (Exception e) {
String message = "An exception occurred inserting a Client User Vendor.";
IExceptionHandlerResponse r = getExceptionHandler().handleData(e, DaoException.class, message);
if (r.isRethrow()) {
DaoException daoe = (DaoException) r.getThrowable();
throw daoe;
}
throw (DaoException)r.getThrowable();
} finally {
}
}
的Junit
public void testInsert() throws Exception {
AppContext.setApplicationContext(applicationContext);
IVendorDto v = new VendorDto();
IClientUserVendorDto userDto = new ClientUserVendorDto();
userDto.setActive(true);
userDto.setClientUserId(new Long(1220));
userDto.setIdentifier("JUnit-ID");
userDto.setVendor(v);
v.setId(new Long(123));
userDto.setCreatedBy("SYSTEM");
userDto.setLastModifiedBy("Junit");
ClientUserVendorDao dao = new ClientUserVendorDao();
dao.setConfiguration(daoConfiguration);
dao.setJdbcTemplate(jdbcTemplate);
BaseJdbcDao baseDao = Mockito.mock(BaseJdbcDao.class);
BaseDao baseDao2 = Mockito.mock(BaseDao.class);
IStatementConfiguration stmtConfiguration = Mockito.mock(IStatementConfiguration.class);
Mockito.when(baseDao.getJdbcTemplate()).thenReturn(jdbcTemplate);
IDaoConfiguration configuration = new DaoConfiguration();
configuration.setStatement(stmtConfiguration);
dao.setConfiguration(configuration);
Mockito.when(baseDao.getJdbcTemplate().queryForObject( Mockito.any(String.class), (RowMapper<Long>) Mockito.any(Object.class))).thenReturn(123L);
userDto = dao.insert(userDto);
}
答案 0 :(得分:0)
我发现了什么问题。 queryForObject未返回long值。所以,这是修复它。
@Test
public void testInsert() throws Exception {
IClientUserVendorDto iClientUserVendorDto = new ClientUserVendorDto();
iClientUserVendorDto.setClientUserId(new Long(1007));
IVendorDto iVendorDto = new VendorDto();
iVendorDto.setId(new Long(1008));
iClientUserVendorDto.setVendor(iVendorDto);
iClientUserVendorDto.setIdentifier("GW_SYS_USER");
iClientUserVendorDto.setActive(true);
iClientUserVendorDto.setCreatedBy("SYSTEM");
iClientUserVendorDto.setLastModifiedBy("SYSTEM");
JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
ClientUserVendorDao clientUserVendroDao2 = Mockito.mock(ClientUserVendorDao.class);
IDaoConfiguration iDaoConfiguration = Mockito.mock(IDaoConfiguration.class);
IStatementConfiguration iStatementConfiguration = Mockito.mock(IStatementConfiguration.class);
Mockito.when(iDaoConfiguration.getStatement()).thenReturn(iStatementConfiguration);
Mockito.when(iStatementConfiguration.getStatement(Mockito.any(String.class))).thenReturn("JUnit");
Mockito.when(clientUserVendroDao2.getJdbcTemplate()).thenReturn(jdbcTemplate);
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Matchers.eq(Long.class))).thenReturn(1007L);
ClientUserVendorDao clientUserVendorDao = new ClientUserVendorDao();
clientUserVendorDao.setConfiguration(iDaoConfiguration);
clientUserVendorDao.setJdbcTemplate(jdbcTemplate);
iClientUserVendorDto = clientUserVendorDao.insert(iClientUserVendorDto);