简体   繁体   English

SolrPing.Process:检查的异常对此方法无效

[英]SolrPing.Process: Checked exception is invalid for this method

I am testing the exception condition for solr ping call. 我正在测试solr ping调用的异常条件。 Here is the code snippet. 这是代码片段。 Solrj version 5.2.0 Solrj版本5.2.0

@Test
public void testPingSolrWhenServerDown2() throws Exception {
  pingSolr = new PingSolr();
  SolrClient mockedSolrClient = Mockito.mock(SolrClient.class);
  SolrPing mockedSolrPing = Mockito.mock(SolrPing.class);
  PowerMockito.whenNew(SolrPing.class).withNoArguments().thenReturn(mockedSolrPing);
  Mockito.doThrow(new IOException()).when(mockedSolrPing).process(mockedSolrClient);
  pingSolr.ping(mockedSolrClient);
}

I am getting the following error: 我收到以下错误:

org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: java.io.IOException
    at org.apache.solr.client.solrj.request.SolrPing.createResponse(SolrPing.java:36)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:134)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:152)
    at c.i.j.s.i.PingSolrTest.testPingSolrWhenServerDown2(PingSolrTest.java:65)

This is saying IOException is an invalid checked exception, which is not allowed with mockito. 这就是说IOException是一个无效的检查异常,mockito不允许这样做。 The same is happening for SolrServerException in another test case. 在另一个测试案例中, SolrServerException也发生了同样的情况。

But SolrPing.process method (extended from SolrRequest ) does throw the exception. 但是SolrPing.process方法(从SolrRequest扩展)确实引发了异常。 SolrRequest.process . SolrRequest.process

One thing I did observe that createResponse does not throw any exception. 我确实观察到的一件事是createResponse不会引发任何异常。 But When I am mocking the process call itself, why it is going to the further deeper calls . 但是, 当我嘲笑process调用本身时,为什么要进行更深层次的调用

The method you are trying to mock 您尝试模拟的方法

public final T process(SolrClient client, String collection)

is marked final which cannot be mocked by Mockito . 被标记为final ,无法被Mockito嘲笑。 Use PowerMockito instead, to mock methods that are static or final . 改用PowerMockito来模拟staticfinal

I would consider to change the approach. 我会考虑改变方法。 I would move method call to Solr into protected method and with spy stub it as you need. 我会将对Solr方法调用移到受保护的方法中,并根据需要使用spy存根。

Sure it is less strong than PowerMock real interaction mocking as well it require to create a dummy method that is package visible. 当然,它不如PowerMock真实的交互模拟强大,它还需要创建一个可见的程序包伪方法。

But it was necessary for us since we had conflict with other test framework and it became habit that I accept and like since I don't need PowerMock anymore 但是对我们来说这是必要的,因为我们与其他测试框架存在冲突,并且成为了我接受和喜欢的习惯,因为我不再需要PowerMock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM