我正在尝试将消息列表发布到其余的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());
}
}
}
请帮我怎么写。
答案 0 :(得分:1)
您可以使用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
然后,您应该在baseUrl
(或其他地方)中定义resourcePath
和application.properties
属性。请记住,服务器将在localhost上运行。
之后你应该模拟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")));
答案 1 :(得分:1)
您可以使用Mockito:
postData
和RestTemplate
Environment
的实例
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
是一个不常见的类名,OP中提供的postJSONData
方法无法编译;它引用meterReadings
而不是data
。
答案 2 :(得分:0)
只需正确模拟 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());
}
}