在 namedParameterJdbcTemplate 中单元测试 GeneratedKeyHolder [英] Unit test GeneratedKeyHolder in namedParameterJdbcTemplate
问题描述
我正在尝试为包含以下代码的函数编写单元测试:
<块引用> KeyHolder newCode = new GeneratedKeyHolder();尝试 {namedParameterJdbcTemplate.update(sql, paramMap, newCode);} catch (DuplicateKeyException e) {logger.error("重复键");}data.setId(newCode.getKey().intValue());
现在,在使用 Mockito 时:
Mockito.when(namedParameterJdbcTemplate.update(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.any(GeneratedKeyHolder.class))).thenReturn(1);
那么我应该如何用数据填充GeneratedKeyHolder
?
谢谢.
我会采用以下方式之一:
- 注入密钥.
- 存根方法调用并在那里设置密钥.
方法一当我说注入密钥时,我实际上是指注入一个 KeyFactory.这意味着您可以在测试中控制结果.例如:
KeyHolder newCode = injectionKeyFactory.getKeyHolder();尝试 {namedParameterJdbcTemplate.update(sql, paramMap, newCode);} catch (DuplicateKeyException e) {logger.error("重复键");}data.setId(newCode.getKey().intValue());
然后在测试中:
KeyHolder newCode = mock(KeyHolder.class);Mockito.when(namedParameterJdbcTemplate.update(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), newCode)).thenReturn(1);Mockito.when(newCode.getKey()).thenReturn(__preferredId__);
密钥工厂是一个简单的工厂,它只返回一个新的 GeneratedKeyHolder.它在构建时被注入,因此此方法确实假设您使用的是 DI.
方法二
Mockito.when(namedParameterJdbcTemplate.update(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.any(GeneratedKeyHolder.class))).thenAnswer(new Answer() {对象应答(InvocationOnMock 调用){Object[] args = invocation.getArguments();映射<字符串,对象>keyMap = new HashMap();keyMap.put("", __preferredId__);((GeneratedKeyHolder)args[2]).getKeyList().add(keyMap);}}).andReturn(1);
我没有真正使用过 Mockito,如果代码不太正确,请见谅.:)
I am trying to write a unit test to a function that holds the following code:
KeyHolder newCode = new GeneratedKeyHolder(); try { namedParameterJdbcTemplate.update(sql, paramMap, newCode); } catch (DuplicateKeyException e) { logger.error("Duplicate Key"); } data.setId(newCode.getKey().intValue());
right now, while using Mockito:
Mockito.when(namedParameterJdbcTemplate.update(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.any(GeneratedKeyHolder.class))).thenReturn(1);
So how am i supposed to fill the GeneratedKeyHolder
with Data?
Thanks.
I would do this in one of the following ways:
- Inject the key.
- Stub the method call and set the key there.
Method 1 When I say inject the key I actually mean inject a KeyFactory. This means you can control the result within your test. For example:
KeyHolder newCode = injectedKeyFactory.getKeyHolder();
try {
namedParameterJdbcTemplate.update(sql, paramMap, newCode);
} catch (DuplicateKeyException e) {
logger.error("Duplicate Key");
}
data.setId(newCode.getKey().intValue());
Then in the test:
KeyHolder newCode = mock(KeyHolder.class);
Mockito.when(namedParameterJdbcTemplate.update(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), newCode)).thenReturn(1);
Mockito.when(newCode.getKey()).thenReturn(__preferredId__);
The key factory is a simple one which just returns a new GeneratedKeyHolder. It gets injected at construction time, so this method does assume you're using DI.
Method 2
Mockito.when(namedParameterJdbcTemplate.update(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.any(GeneratedKeyHolder.class))).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
Map<String, Object> keyMap = new HashMap<String, Object>();
keyMap.put("", __preferredId__);
((GeneratedKeyHolder)args[2]).getKeyList().add(keyMap);
}
}).andReturn(1);
I've not really used Mockito, so sorry if the code's not quite right. :)
这篇关于在 namedParameterJdbcTemplate 中单元测试 GeneratedKeyHolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!