我尝试为工作应用程序(spring,hibernate,soap,cxf)实现集成测试。我手工构建SOAP-XML并将其处理到我的端点。像这样:
private Source createRequest(String domainName, String email, String userId, String password) {
String content = "";
content += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sup=\"[...]">";
content += " <soapenv:Header/>";
content += " <soapenv:Body>";
content += " <sup:RegisterRequest>";
content += " <sup:domain>" + domainName + "</sup:domain>";
content += " <sup:email>" + email + "</sup:email>";
content += " <sup:userId>" + userId + "</sup:userId>";
content += " <sup:password>" + password + "</sup:password>";
content += " </sup:RegisterRequest>";
content += " </soapenv:Body>";
content += "</soapenv:Envelope>";
return new StringSource(content);
}
在我的测试中,我将其处理到我的端点,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "../../WEB-INF/applicationContext.xml", "../../WEB-INF/test.xml" })
@Transactional
public class TestRegisterEndpoint extends AbstractTransactionalJUnit4SpringContextTests {
@Resource
private RegisterEndpoint registerEndpoint;
@Test
public void registerUserFailsForUnexistantDomain() throws Exception {
Source result = registerEndpoint.invoke(createRequest("unexistant_domain", "test@test.de", "test@test.de", "myPassword1"));
}
}
但是当我尝试运行测试时,我得到一个异常“无法找到元素声明'soapenv:Envelope'。”。
org.springframework.oxm.jaxb.JaxbUnmarshallingFailureException: JAXB unmarshalling exception: null; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'soapenv:Envelope'.]
at org.springframework.oxm.jaxb.JaxbUtils.convertJaxbException(JaxbUtils.java:75)
[...]
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'soapenv:Envelope'.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
[...]
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'soapenv:Envelope'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
我猜我必须在某个地方定义“soapenv”命名空间。但如果是这样,我不知道在哪里。
“applicationContext.xml”的开头:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
“test.xml”的开头:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
如何摆脱异常?
答案 0 :(得分:2)
好的,我在其他地方找到了帮助。解决方案:因为我的端点扩展了“PayloadEndpoint”
public interface RegisterEndpoint extends PayloadEndpoint {
}
我必须只将有效负载插入端点(听起来合乎逻辑;))。如果我这样做:
private Source createRequest(String domainName, String email, String userId, String password) {
String content = "";
content += "<sup:RegisterRequest xmlns:sup=\"[...]\">";
content += " <sup:domain>" + domainName + "</sup:domain>";
content += " <sup:email>" + email + "</sup:email>";
content += " <sup:userId>" + userId + "</sup:userId>";
content += " <sup:password>" + password + "</sup:password>";
content += "</sup:PreregisterRequest>";
return new StringSource(content);
}
一切正常。