简体   繁体   English

如何使用Mockito模拟静态方法以进行单元测试

[英]How to mock the static method with mockito to do unit test

I have a method like this. 我有这样的方法。

 public Response post(String json) {

   EventList list = Recorder.getRecorders();
   if (null == list || list.isEmpty()) {
     throw new ServiceUnavailableException("An Recorder is either not configured");
  }
  String targetIUrl = list.getNext().getBase();
  String targetV2Url = targetUrl + "/v2";

 // other processes......
}
  1. I want to mock Recorder.getRecorder() and do something like when(Recorder.getRecorder()).thenReturn(null) and test if throw a 503 exception. 我想模拟Recorder.getRecorder()并执行when(Recorder.getRecorder())。thenReturn(null)之类的操作,并测试是否抛出503异常。 But getRecorder() is a static method. 但是getRecorder()是静态方法。 I know Mockito cannot mock the static method, but I still wanna know if it is possible to change some code made this testable without using Powermock or other libraries. 我知道Mockito无法模拟静态方法,但是我仍然想知道是否有可能在不使用Powermock或其他库的情况下更改一些可测试的代码。

  2. If I mock Recorder, do I have to change the method to post(String json, Recorder recorder)? 如果我嘲笑Recorder,是否必须将方法更改为post(String json,Recorder records)? Otherwise, how can I make this mock interact with the method? 否则,如何使该模拟与方法交互?

If you want to mock the getRecorders() behaviour without using a library for mocking static methods (such as Powermock) then you'll have to extract the static call from inside post() . 如果要模拟getRecorders()行为而不使用模拟静态方法的库(例如Powermock),则必须从post()内部提取静态调用。 There are a few options for this: 有几种选择:

  1. Pass the EventList into post() EventList传递到post()

     public post(String json, EventList list) { ... } 
  2. Inject the EventList into the class which contains post() EventList注入到包含post()的类中

     public class TheOneThatContainsThePostMethod { private EventList eventList; public TheOneThatContainsThePostMethod(EventList eventList) { this.eventList = eventList; } public post(String json) { if (null == this.eventList || this.eventList.isEmpty()) { throw new ServiceUnavailableException("An Recorder is either not configured"); } } } 
  3. Hide the static method call inside another class and inject an instance of that class into post() or the class which contains post() . 将静态方法调用隐藏在另一个类中,然后将该类的实例注入post()或包含post()的类中。 For example: 例如:

     public class RecorderFactory { public EventList get() { return Recorder.getRecorders(); } } public class TheOneThatContainsThePostMethod { private RecorderFactory recorderFactory; public TheOneThatContainsThePostMethod(RecorderFactory recorderFactory) { this.recorderFactory = recorderFactory; } public post(String json) { EventList list = recorderFactory.getRecorders(); ... } } // Or ... public post(String json, RecorderFactory recorderFactory) { EventList list = recorderFactory.getRecorders(); ... } 

With the first two approaches your test can simply invoke post() providing (1) a null EventList ; 使用前两种方法,您的测试可以简单地调用post()从而提供(1)空的EventList (2) an empty EventList ... thereby allowing you to test the 'throw a 503 exception' behaviour. (2)一个空的EventList ...,从而使您可以测试“引发503异常”行为。

With the third approach you can use Mockito to mock the behaviour of the RecorderFactory to return (1) a null EventList ; 使用第三种方法,您可以使用Mockito模拟RecorderFactory的行为以返回(1)空的EventList (2) an empty EventList ... thereby allowing you to test the 'throw a 503 exception' behaviour. (2)一个空的EventList ...,从而使您可以测试“引发503异常”行为。

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

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