我正在使用以下wiremock依赖项:
com.github.tomakehurst 线模独立测试 2.6.0
当通过选择 run as JunitTest 从 eclipse 运行时,一切正常。但是当我从 maven 运行这个程序时,我得到了以下错误。
wiremock.com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.github.tomakehurst.wiremock.stubbing.StubMapping["request"]->com.github.tomakehurst.wiremock.matching.RequestPattern["bodyPatterns"]->java.util.ArrayList[0])
at com.example.integration.controller.FileUploadControllerIntegrationTest.configureSystemUnderTest(FileUploadControllerIntegrationTest.java:47)
Caused by: java.lang.NullPointerException
at com.example.integration.controller.FileUploadControllerIntegrationTest.configureSystemUnderTest(FileUploadControllerIntegrationTest.java:47)
我有以下引发错误的代码。
package com.example.integration.controller;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static com.example.integration.controller.TestConstants.*;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.example.Application;
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
@import(TestConfig.class)
@testinstance(Lifecycle.PER_CLASS)
public class FileUploadControllerIntegrationTest extends GenericIntegrationTest {
@Autowired
MockMvc mockMvc;
private WireMockServer wireMockServer;
@BeforeAll
void configureSystemUnderTest() {
this.wireMockServer = new WireMockServer(options().port(8090).notifier(new ConsoleNotifier(true)));
this.wireMockServer.start();
}
@AfterAll
void stopWireMockServer() {
this.wireMockServer.stop();
}
@DisplayName("upload and validate with success")
@Test
public void testUploadFile() throws Exception {
MvcResult result = mockMvc.perform(createRequest(BCM_DEPLOY_SCOPE, "integration-payload/order-type.json"))
.andExpect(status().isCreated()).andReturn();
assertNotNull(result.getResponse().getContentAsString());
}
@DisplayName("upload and validate with validation failed.")
@Test
public void uploadFileWithValidationFail() throws Exception {
mockMvc.perform(createRequest(BCM_DEPLOY_SCOPE, "integration-payload/order-type-with-invalid.json"))
.andExpect(status().isBadRequest())
.andExpect(content().json(populateJsonToString("__files/json/validation-failure-response.json")));
}
/**
* @param endpoint
* @param multipartFile
* @param headers
* @return
* @throws IOException
* @throws JSONException
*/
private MockHttpServletRequestBuilder createRequest(String scope, String payloadFile)
throws IOException, JSONException {
String endpoint = "http://localhost:8080/files/upload";
InputStream fis = new ClassPathResource(payloadFile).getInputStream();
MockMultipartFile multipartFile = new MockMultipartFile("file", fis);
HttpHeaders headers = getHeadersUsingScope(scope);
return multipart(endpoint).file(multipartFile).contentType(MediaType.MULTIPART_FORM_DATA).headers(headers);
}
}
这是我正在使用的映射文件有效负载。
{
"request":{
"method":"POST",
"headers":{
"Authorization":{
"contains":"Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhel9hdHRyIjp7ImFwcGxpY2F0aW9uX25hbWUiOiJSZWdTZXJ2aWNlIiwiYXBwbGljYXRpb25faWQiOiJCQ1MifSwiemlkIjoic2FwLWljYnMiLCJ1c2VyX2lkIjoidGVzdFVzZXIiLCJ1c2VyX25hbWUiOiJ1c2VyIG5hbWUiLCJzY29wZSI6WyJ1YWEtc2VydmljZS1iY3MuQnVzQ29uZmlnbk1hbmFnZUNvbnRlbnQiXSwiZXhwIjoyMTQ3NDgzNjQ3LCJjbGllbnRfaWQiOiJzYi1CQ1NTZXJ2aWNlIiwiZW1haWwiOiJ0ZXN0VXNlckB0ZXN0T3JnIiwiY2lkIjoic2ItQkNTU2VydmljZSJ9.ax5hEOLjmazyXKak9GHnQ3ZswQbL2rkRv2fM6p5rD3NPeUKPO6IaXTImGNCwk3wk6UbkTXuQu8rOQzHugvMLajYwFTpeMuOMjCZ-B8MgrNZq2EKiKe6aQKfzZ6vJZkPWon2UuZWNfKFxbBtglcVi4rL4Z655IWu4zWJ9_r6ruHVybW8eU4hD-4UNdJB2m4pISJw1usqA-_GaMMfQ5j6azx93m1qHuWUYMMmTtq_JefHiA4FueYHh4cBj-aHvTa_PYkG8rg0qY6dvJPw1R9ANKNG5CsQZmzUsY3a9KZX5wgRoSYshePj25mR1GmOzb8PCIDUmyWST-_keF0OnnqJhyQ"
},
"Content-Type":{
"contains":"application/json"
},
"Accept-Language":{
"contains":"en"
}
},
"url":"/busconfig/validate",
"bodyPatterns":[
{
"equalToJson":{
"serviceEntities":{
"OrderType":{
"data":[
{
"Code":"TE",
"BusConfignIsDeleted":false,
"BusConfignValidStart":"/Date(1571356800000)/",
"BusConfignValidEnd":"/Date(1571356800000)/"
},
{
"Code":"YZ",
"BusConfignIsDeleted":false,
"BusConfignValidStart":"/Date(1572739200000)/",
"BusConfignValidEnd":"/Date(1575072000000)/"
}
],
"entityEndPoint":"SalesOrderOrderTypeService/OrderType"
},
"OrderTypeText":{
"data":[
{
"Code":"TE",
"BusConfignIsDeleted":false,
"LanguageCode":"en",
"LanguageName":"English",
"Description":"TE Desc"
},
{
"Code":"YZ",
"BusConfignIsDeleted":false,
"LanguageCode":"en",
"Description":"Validation error code"
}
],
"dependency":[
"OrderType"
],
"entityEndPoint":"SalesOrderOrderTypeService/OrderTypeText"
}
},
"required":[
"OrderType",
"OrderTypeText"
],
"description":"Check",
"changeType":"m"
}
}
]
},
"response":{
"status":200
}
}