gpt4 book ai didi

java - 如何模拟 jdbcTemplate.execute(callableStatementCreator, callableStatementCallback);

转载 作者:行者123 更新时间:2023-12-02 09:39:48 24 4
gpt4 key购买 nike

我正在尝试模拟(Spring boot、JUnit、Oracle)

jdbcTemplate.execute(CallableStatementCreator, CallableStatementCallback);

public class ExceptionTest
{
@Autowired
private SecurityDAOImpl securityDAOImplMock;

@Mock
private JdbcTemplate jdbcTemplate;

@Autowired
private JdbcTemplate resetJdbcTemplate;

@Before
public void init() throws Exception
{
securityDAOImplMock = spy(new SecurityDAOImpl());
MockitoAnnotations.initMocks(this);
}

@SuppressWarnings("unchecked")
@Test(expected = SecurityDAOException.class)
public void testUpdateProfileException()
{
DataAccessException dataAccessException = new DataAccessException("Mock Exception",
new Exception("Mocked DataAccessException"))
{
private static final long serialVersionUID = 1L;
};
ReflectionTestUtils.setField(securityDAOImplMock, "jdbcTemplate", jdbcTemplate);
doThrow(dataAccessException).when(jdbcTemplate).execute(any(), any());
securityDAOImplMock.isTooManyFailedAttempt("", 7, "", "");
}

@After
public void reset()
{
ReflectionTestUtils.setField(securityDAOImplMock, "jdbcTemplate", resetJdbcTemplate);
}
}

我收到以下编译时异常:

The method execute(PreparedStatementCreator, PreparedStatementCallback<Object>) is ambiguous for the type 

在这条线上

doThrow(securityDAOException).when(jdbcTemplate).execute(any(), any());

如何模拟 jdbcTemplate.execute(callableStatementCreator, callableStatementCallback)。如何使其工作?

最佳答案

方法JdbcTemplate.execute()overloaded .

因此,当您使用匹配器 any() 模拟它时,编译器根本不知道您实际指的是哪个方法并抛出错误。

要修复此问题,请在匹配器中提供一个类来解决此歧义。

例如,如果你想模拟

JdbcTemplate.execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action)

使用

doThrow(securityDAOException).when(jdbcTemplate).execute(any(PreparedStatementCreator.class), any(PreparedStatementCallback.class));

关于java - 如何模拟 jdbcTemplate.execute(callableStatementCreator, callableStatementCallback);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57191211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com