简体   繁体   English

使用MockRestServiceServer模拟REST调用

[英]Mocking a REST call with MockRestServiceServer

I'm trying to write a JUnit test case which tests a method in a helper class. 我正在尝试编写一个JUnit测试用例,用于测试辅助类中的方法。 The method calls an external application using REST and it's this call that I am trying to mock in the JUnit test. 该方法使用REST调用外部应用程序,这是我试图在JUnit测试中模拟的调用。

The helper method makes the REST call using Spring's RestTemplate. 辅助方法使用Spring的RestTemplate进行REST调用。

In my test, I create a mock REST server and mock REST template and instanitiate them like this: 在我的测试中,我创建了一个模拟REST服务器并模拟REST模板并将它们实例化为:

@Before
public void setUp() throws Exception {
    mockServer = MockRestServiceServer.createServer(helperClass.getRestTemplate());
}

I then seed the mock server so that it should return an appropriate response when the helper method makes the REST call: 然后我为mock服务器播种,以便在helper方法进行REST调用时它应该返回一个适当的响应:

// response is some XML in a String
mockServer
    .expect(MockRestRequestMatchers.requestTo(new URI(myURL)))
    .andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
    .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
        .contentType(MediaType.APPLICATION_XML)
        .body(response));

When I run my test, the helper method receives a null response from the REST call it makes and the test fails. 当我运行我的测试时,helper方法从它所做的REST调用中接收一个空响应,并且测试失败。

The REST URL that the helper makes has query params and looks like this: " http://server:port/application/resource?queryparam1=value1&queryparam2=value2 ". 帮助程序生成的REST URL具有查询参数,如下所示:“ http:// server:port / application / resource?queryparam1 = value1&queryparam2 = value2 ”。

I've tried putting the URL (" http://server:port/application/resource ") both with and without the query parameters in the "myURL" variable (to elicit a match so that it returns a response), but can not get the mock server to return anything. 我已经尝试将带有不带有查询参数的URL(“ http:// server:port / application / resource ”)放在“myURL”变量中(以引出匹配以便它返回响应),但是可以不让模拟服务器返回任何东西。

I've tried searching for examples of this kind of code but have yet to find anything which seems to resemble my scenario. 我试过搜索这种代码的例子,但还没有找到任何看起来像我的场景的东西。

Spring version 4.1.7. Spring版本4.1.7。

Thanks in advance for any assistance. 在此先感谢您的任何帮助。

When you create an instance of MockRestServiceServer you should use existing instance of RestTemplate that is being used by your production code. 创建MockRestServiceServer实例时,应使用生产代码正在使用的现有RestTemplate实例。 So try to inject RestTemplate into your test and use it when invoking MockRestServiceServer.createServer - don't create new RestTemplate in your tests. 因此,尝试将RestTemplate注入到测试中并在调用MockRestServiceServer.createServer时使用它 - 不要在测试中创建新的RestTemplate

Seems that you are trying to test the rest-client, the rest-server should be tested in other place. 似乎您正在尝试测试rest-client,其余服务器应该在其他地方进行测试。 You are using RestTemplate -> To call the service. 您正在使用RestTemplate - >调用该服务。 Then, tried to mock RestTemplate and its call's results. 然后,尝试模拟RestTemplate及其调用的结果。

@Mock
RestTemplate restTemplateMock;

and Service Under Test Class 和测试类服务

@InjectMocks
Service service;

Let say, Service has a method to be test as 比方说,Service有一种方法可以测试

public void filterData() {
   MyResponseModel response = restTemplate.getForObject(serviceURL, MyResponseModel.class);
   // further processing with response
}

Then, to test filterData method, you need to mock the response from restTemplate call such as 然后,要测试filterData方法,您需要模拟来自restTemplate调用的响应,例如

mockResponseModel = createMockResponse();
Mockito.when(restTemplateMock.getForObject(serviceURL, MyResponseModel.class)).thenReturn(mockResponseModel);

service.filterData();
//Other assert/verify,... go here

You can create a new instance of RestTemplate however you have to pass it in your createServer method like this: 您可以创建RestTemplate的新实例,但是您必须在createServer方法中传递它,如下所示:

private RestTemplate restTemplate = new RestTemplate();
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
mockServer = MockRestServiceServer.createServer(restTemplate);
client.setRestTemplate(restTemplate);
}

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

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