如何为 Resttemplate postForObject 方法编写 mockito junit

How to write mockito junit for Resttemplate postForObject method

我正在尝试 post 将消息列表发送给其余 api。如何为下面的方法 postJSONData 编写 mockito junit:

public class PostDataService{

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private Environment env;

    private HttpEntity<String> httpEntity;

    private HttpHeaders httpHeaders;

    private String resourceURL = null;

    public PostDataService(){
    httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    }

    public void postJSONData(List<String> data){
    try
    {
    resourceURL = env.getProperty("baseURL") + env.getProperty("resourcePath");
    httpEntity = new HttpEntity<String>(data.toString(), httpHeaders);
    String response = restTemplate.postForObject(resourceURL, httpEntity, String.class);
    }
    catch (RestClientException e) {
            LOGGER.info("ErrorMessage::" + e.getMessage());
            LOGGER.info("ErrorCause::" + e.getCause());
        }
    } 


}

请大家帮我写一下

您可以使用wiremock 模拟服务器。这是一个专门针对这项工作的模拟框架。

将以下依赖项添加到您的pom.xml:

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock</artifactId>
    <version>2.12.0</version>
</dependency>

将以下规则添加到您的测试中:

@Rule
public WireMockRule wireMockRule = new WireMockRule(); // default port is 8080

然后您应该在 application.properties(或其他地方)定义您的 baseUrlresourcePath 属性。请记住,服务器将在本地主机上 运行。

之后你应该为 resourcePath 模拟 HTTP 响应:

stubFor(get(urlEqualTo(resourcePath))
            .withHeader("Accept", equalTo("application/json"))
            .willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBody(content)));

然后就可以执行postJSONData方法了:

postData.postJSONData();

最后,您可以验证对服务器的请求是否正确。

verify(postRequestedFor(urlMatching(resourcePath))
        .withRequestBody(matching(expectedBody))
        .withHeader("Content-Type", matching("application/json")));

您可以使用 Mockito 来:

  • 使用模拟的 RestTemplateEnvironment
  • 创建 postData 的实例
  • 对这些设置期望以允许 ``postJSONData` 调用完成
  • 验证模拟的 RestTemplate 是否被正确调用

postJSONData 方法不使用 restTemplate.postForObject() 响应,因此在测试此方法方面,您可以做的最好的事情是验证是否使用正确的参数调用了 restTemplate.postForObject()

这是一个例子:

@RunWith(MockitoJUnitRunner.class)
public class PostDataTest {

    @Mock
    private RestTemplate restTemplate;
    @Mock
    private Environment env;

    @InjectMocks
    private PostData postData;

    @Test
    public void test_postJSONData() {
        String baseUrl = "theBaseUrl";
        String resourcePath = "aResourcePath";

        Mockito.when(env.getProperty("baseURL")).thenReturn(baseUrl);
        Mockito.when(env.getProperty("resourcePath")).thenReturn(resourcePath);

        List<String> payload = new ArrayList<>();

        postData.postJSONData(payload);

        // it's unclear from your posted code what goes into the HttpEntity so
        // this approach is lenient about its expectation
        Mockito.verify(restTemplate).postForObject(
                Mockito.eq(baseUrl + resourcePath),
                Mockito.any(HttpEntity.class),
                Mockito.eq(String.class)
        );

        // assuming that the HttpEntity is constructed from the payload passed 
        // into postJSONData then this approach is more specific
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");
        Mockito.verify(restTemplate).postForObject(
                Mockito.eq(baseUrl + resourcePath),
                Mockito.eq(new HttpEntity<>(payload.toString(), headers)),
                Mockito.eq(String.class)
        );
    }
}

旁注; postData 是 class 的不寻常名称,您的 OP 中提供的 postJSONData 方法无法编译;它引用 meterReadings 而不是 data

只需正确模拟 postForObject

    @ExtendWith(MockitoExtension.class)
    public class YourServiceTest {
        
            @Mock
            RestTemplate template;
        
            @InjectMocks
            private final YourService srv = new YourService();
        
        
            @Test
            public void yourTest() {
                when(template.postForObject(anyString(),any(Object.class),eq(String.class)))
                        .thenReturn("xxxxxxxxxxx");
                assertEquals("xxxxxxxxxxx", srv.yourMethod());
            }
        }