我已经创建了一个简单的Java Web应用程序,并且正在使用Gradle构建一个war文件。我的战争正在部署到Cloud Foundry Cloud。到目前为止,一切都很好。为了让您更好地了解我想做什么:
POST请求(正文包含SubmitRequest.xml的内容)
https://mywebapponcloudfoundry.com/submit
响应异常(与Wiremock模拟)
HTTP/1.1 200 OK with content of 'SubmitResponse.xml' as response
代码块1的实际响应
com.github.tomakehurst.wiremock.security.NotAuthorisedException: access to file /SubmitResponse.xml is not permitted
代码块1
static {
//THIS CODE BLOCK IS PART OF A WEB SERVLET (Javax Servlet)
final ResponseTemplateTransformer theTemplateTransformer =
new ResponseTemplateTransformer(false);
templateTransformerName = theTemplateTransformer.getName();
mWireMockServer = new WireMockServer(
WireMockConfiguration
.options()
.withRootDirectory("src/main/resources/")
.extensions(theTemplateTransformer)
);
mWireMockServer.start();
stubFor(post(urlEqualTo("/submit"))
.withRequestBody(containing("<message-type>SubmitRequest</message-type>"))
.willReturn(aResponse()
.withBodyFile("SubmitResponse.xml")
.withStatus(200)
.withTransformers(templateTransformerName)
));
}
代码块2的实际响应
java.io.FileNotFoundException: /home/vcap/app/src/main/resources/__files/SubmitResponse.xml (No such file or directory)
代码块2中的差异
//instead of .withBodyFile("/SubmitResponse.xml")
.withBodyFile("SubmitResponse.xml")
要进行复制,请考虑进行以下JUnit测试。 (有效!)
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static io.restassured.config.XmlConfig.xmlConfig;
public class Test {
private static WireMockServer mWireMockServer;
private static String templateTransformerName;
/**
* Logs the HTTP status,the HTTP headers and the body from the supplied response.
*
* @param inResponseEntity Response for which to log information.
*/
protected void logResponseStatusHeadersAndBody(final Response inResponseEntity) {
System.out.println("Response status: {}" + inResponseEntity.getStatusCode());
System.out.println("Response headers: {}" + inResponseEntity.getHeaders());
System.out.println("Response body: " + inResponseEntity.getBody().asString());
}
@Before
public void setup() {
RestAssured.reset();
RestAssured.port = 8080;
final ResponseTemplateTransformer theTemplateTransformer =
new ResponseTemplateTransformer(false);
templateTransformerName = theTemplateTransformer.getName();
mWireMockServer = new WireMockServer(
WireMockConfiguration
.options()
//.withRootDirectory("src/main/resources/") is not needed here,because Wiremock takes src/test/resources as default value
.extensions(theTemplateTransformer));
mWireMockServer.start();
}
@Test
public void testSubmit() throws ServiceException {
stubFor(post(urlEqualTo("/submit"))
.withRequestBody(containing("<message-type>SubmitRequest</message-type>"))
.willReturn(aResponse()
.withBodyFile("SubmitResponse.xml")
.withStatus(200)
.withTransformers(templateTransformerName)
));
final Response theResponse = RestAssured
.given()
.config(RestAssured.config().xmlConfig(xmlConfig().with().namespaceAware(false)))
.contentType(ContentType.XML)
.body(new File("SubmitRequest.xml"))
.when()
.post("/submit");
logResponseStatusHeadersAndBody(theResponse);
}
}
当我在Cloud Environment上遇到异常时,为什么JUnit测试中的代码可以正常工作?谁能帮我吗?