繁体   English   中英

如何在 JAX-RS 中模拟 HttpUrlConnection

[英]How to mock HttpUrlConnection in JAX-RS

我正在尝试对我的 class 进行模拟测试,但它不起作用。

原装class:

public class Service {
  .................
  @POST
  ...
  public Message Info(@NotNull @Valid final UUID Id) throws IOException {
    ............
    String httpLink = Config.getLink();
    ...
    if ( ...) { 
      HttpURLConnection urlConnection = null;
      URL url = new URL(OriginalLink);
      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("GET");
      urlConnection.setUseCaches(false);
      urlConnection.setConnectTimeout(50000);
      urlConnection.setReadTimeout(50000);
      urlConnection.setRequestProperty("Content-Type", "application/json");
      urlConnection.connect();
      .................
      String jsonobject = "..."; //data to be posted into api
      .......
    } else {
      return new Message("No data to posted from file");
    }
    return new Message("successfully posted data from file");
  }
}
  

测试 class:

@Before
public void before() {
  MockitoAnnotations.initMocks(this);
  URL u = PowerMockito.mock(URL.class);
  String url = "url";
  PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);
  HttpURLConnection huc = PowerMockito.mock(HttpURLConnection.class);
  PowerMockito.when(u.openConnection()).thenReturn(huc);
  PowerMockito.when(huc.getResponseCode()).thenReturn(200);
  .....
  PowerMockito.when(Config.getLink())
    .thenReturn("url");
}
 @Test
 public void testInfo() throws IOException {
    ............
    Message org = new Message("successfully posted data from file");
    Message msg = Service.Info(uuid);
    assertEquals(msg,org);
 }

我尝试使用 Mockito 和 PowerMockRunner 进行模拟,但没有成功。 使用 PowerMockito,我得到“java.lang.NullPointerException”

使用您的实现,您无法模拟HttpURLConnection ,因为它是在URL的不可模拟实例中创建的,该实例是在您的方法中使用new URL(OriginalLink);

You would have to create the URL with some kind of factory that you inject into the service, then you can mock that service to provide a mocked URL instance, and that mocked URL instance would have to provide a mocked HttpURLConnection instance.

或者,您将创建与 URL 的连接的部分提取到一个额外的方法中,该方法获取 URL 并返回连接的 HttpURLConnection。 然后,您可以额外测试该方法并模拟或“监视”它以测试另一种方法。

就像是

class ConnectionService {
  HttpURLConnection openConnection(URL connectTo) {
    HttpURLConnection urlConnection = (HttpURLConnection) connectTo.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("GET");
    urlConnection.setUseCaches(false);
    urlConnection.setConnectTimeout(50000);
    urlConnection.setReadTimeout(50000);
    urlConnection.setRequestProperty("Content-Type", "application/json");
    urlConnection.connect();
    return urlConnection;
  }
}

然后在您的代码中:

// get connectionService injected
URL url = new URL(OriginalLink);
HttpURLConnection urlConnection = connectionService.openConnection(url);

暂无
暂无

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

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