如何模拟 jdbcTemplate.execute(callableStatement);

我正在尝试模拟(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)。如何让它工作?


慕容708150
浏览 150回答 1
1回答

慕斯王

方法JdbcTemplate.execute()重载。_因此,当您使用匹配器模拟它时,any()编译器根本不知道您实际指的是哪种方法并抛出错误。要解决这个问题,请在匹配器中提供一个类来解决这种歧义。例如,如果你想模拟JdbcTemplate.execute(PreparedStatementCreator&nbsp;psc,&nbsp;PreparedStatementCallback<T>&nbsp;action)使用doThrow(securityDAOException).when(jdbcTemplate).execute(any(PreparedStatementCreator.class),&nbsp;any(PreparedStatementCallback.class));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java