有应用程序spring(web)+ jaxb + maven + jpa
集成测试在本地成功通过,但是,当尝试在远程服务器(jenkins + tomcat)上启动集成测试时,测试失败,并带有ResourceAccessException异常和嵌套的FileNotFound异常。
詹金斯台阶
mvn clean package
mvn test
我清除了从文件读取数据并对其进行硬编码的所有点,但是错误是相同的。
pom.xml:
...
<build>
...
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources/xml</directory>
</testResource>
</testResources>
<testSourceDirectory>src/test</testSourceDirectory>
</build>
如何解决此问题?
stackTrace:
Tests run: 6, Failures: 0, Errors: 4, Skipped: 0, Time elapsed: 1.166 sec <<< FAILURE!
2018-10-24 17:53:39.742 | findCustomers_byThreeParams(integration.CustomerControllerTest) Time elapsed: 0.001 sec <<< ERROR!
2018-10-24 17:53:39.742 | org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://server-at-1.myCompany:29808/MyApp/Customers": http://server-at-1.myCompany:29808/MyApp/Customers?businessProcessId=2a201bff&customerObjectId=111&systemId=pega&CustomerType=FFF; nested exception is java.io.FileNotFoundException: http://server-at-1.myCompany:29808/MyApp/Customers?businessProcessId=2a201bff&customerObjectId=111&systemId=pega&CustomerType=FFF
2018-10-24 17:53:39.742 | at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
2018-10-24 17:53:39.742 | at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:690)
2018-10-24 17:53:39.742 | at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:453)
2018-10-24 17:53:39.742 | at integration.CustomerControllerTest.addCustomer(CustomerControllerTest.java:154)
2018-10-24 17:53:39.742 | at integration.CustomerControllerTest.findCustomers_byThreeParams(CustomerControllerTest.java:109)
2018-10-24 17:53:39.742 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2018-10-24 17:53:39.743 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2018-10-24 17:53:39.743 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2018-10-24 17:53:39.743 | at java.lang.reflect.Method.invoke(Method.java:498)
2018-10-24 17:53:39.743 | at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
2018-10-24 17:53:39.743 | at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
2018-10-24 17:53:39.743 | at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
2018-10-24 17:53:39.743 | at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
2018-10-24 17:53:39.743 | at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
2018-10-24 17:53:39.743 | at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
2018-10-24 17:53:39.743 | at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
2018-10-24 17:53:39.743 | at org.junit.runners.Suite.runChild(Suite.java:128)
2018-10-24 17:53:39.743 | at org.junit.runners.Suite.runChild(Suite.java:27)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
2018-10-24 17:53:39.743 | at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
2018-10-24 17:53:39.743 | at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
2018-10-24 17:53:39.743 | at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:62)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:139)
2018-10-24 17:53:39.743 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2018-10-24 17:53:39.743 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2018-10-24 17:53:39.743 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2018-10-24 17:53:39.743 | at java.lang.reflect.Method.invoke(Method.java:498)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
2018-10-24 17:53:39.743 | at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
2018-10-24 17:53:39.743 | Caused by: java.io.FileNotFoundException: http://server-at-1.myCompany:29808/MyApp/Customers?businessProcessId=2a201bff&customerObjectId=111&systemId=pega&CustomerType=FFF
2018-10-24 17:53:39.743 | at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1890)
2018-10-24 17:53:39.743 | at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
2018-10-24 17:53:39.743 | at org.springframework.http.client.SimpleClientHttpResponse.getBody(SimpleClientHttpResponse.java:88)
2018-10-24 17:53:39.743 | at org.springframework.http.client.BufferingClientHttpResponseWrapper.getBody(BufferingClientHttpResponseWrapper.java:71)
2018-10-24 17:53:39.743 | at integration.config.LoggingRequestInterceptor.traceResponse(LoggingRequestInterceptor.java:37)
2018-10-24 17:53:39.743 | at integration.config.LoggingRequestInterceptor.intercept(LoggingRequestInterceptor.java:22)
2018-10-24 17:53:39.743 | at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
2018-10-24 17:53:39.743 | at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76)
2018-10-24 17:53:39.743 | at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
2018-10-24 17:53:39.743 | at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
2018-10-24 17:53:39.743 | at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:734)
2018-10-24 17:53:39.743 | ... 52 more
2018-10-24 17:53:39.743 |
Jaxb实体:
import lombok.Getter;
import lombok.Setter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
public class Customer implements Serializable {
@XmlElement(name = "base", required = true)
private base base;
@XmlElement(name = "ext", required = false)
private ext ext;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "base")
@Getter
@Setter
public static class base implements Serializable {
@XmlElement(name = "Value")
BigDecimal value;
@XmlElement(name = "Type")
String type;
@XmlElement(name = "ObjectId")
String objectId;
@XmlElement(name = "name")
String name;
@XmlElement(name = "DateTimeCalc")
@XmlJavaTypeAdapter(DateAdapter.class)
Date dateTimeCalc;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ext")
@Getter
@Setter
public static class ext implements Serializable {
@XmlElement(name = "Code")
String code;
@XmlElement(name = "Value")
String value;
}
}
控制器:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class CustomerController {
@Autowired
private CustomerService CustomerService;
@RequestMapping(value = "/Customers", method = RequestMethod.GET)
public @ResponseBody
CustomersWrapper findCustomers(@RequestParam(value = "businessProcessId") String businessProcessId,
@RequestParam(value = "customerObjectId") String customerObjectId,
@RequestParam(value = "CustomerType", required = false) String CustomerType) throws Exception {
return (new CustomersWrapper(CustomerService.findCustomers(businessProcessId, customerObjectId, CustomerType)));
}
@RequestMapping(value = "/Customers", method = RequestMethod.POST)
public void addCustomer(@RequestBody Customer Customer,
@RequestParam(value = "businessProcessId") String businessProcessId,
@RequestParam(value = "customerObjectId") String customerObjectId,
@RequestParam(value = "productSystemId", required = false) String productSystemId) {
CustomerService.addCustomer(businessProcessId, customerObjectId, productSystemId, Customer.getBaseParam().getType(), Customer);
}
@RequestMapping(value = "/greeting", method = RequestMethod.GET)
public String greeting() {
return CustomerService.greeting();
}
}
我的测试是:
@Before
public void before() throws ClassNotFoundException, SQLException {
logger.log(WARN, "in before..");
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@data-at-1.myCompany:29152:orcl",
"TTTTT",
"TTTTT");
connection.createStatement().executeQuery("TRUNCATE TABLE CustomerTABLE");
serverAddress = getServerAddress();
logger.log(WARN, "end before..");
}
@After
public void after() throws SQLException {
logger.log(WARN, "@after started..");
connection.close();
logger.log(WARN, "@after end..");
}
@Test
public void addCustomer() throws JAXBException, SQLException {
logger.log(WARN, "addCustomer start..");
Customer Customer1 = new Customer();
Customer.Base baseParam = new Customer.Base();
baseParam.setType("FFF");
baseParam.setDateTimeCalc(new Date());
baseParam.setModelName("fff");
baseParam.setObjectId("fdfsf");
baseParam.setValue(new BigDecimal(13242));
Customer.Ext extParam = new Customer.Ext();
Customer1.setExtParam(extParam);
Customer1.setBaseParam(baseParam);
Customer Customer2 = new Customer();
Customer2.setExtParam(extParam);
Customer2.setBaseParam(baseParam);
sendPost(serverAddress + "/Customers?businessProcessId=2a201bff&CustomerObjectId=111&productSystemId=pega", Customer1);
sendPost(serverAddress + "/Customers?businessProcessId=ffffffff&CustomerObjectId=222&productSystemId=pega", Customer2);
logger.log(WARN, "addCustomer end..");
}
现在,我认为,restTemplate解决了该问题。 我可以在正文中发送带有xml的帖子,但是如果尝试通过restTemplate发送,则会出现FileNotFoundException。