gpt4 book ai didi

spring - 通过 junit 测试 SOAP 端点会抛出 SAXParseException (soapenv :Envelope)

转载 作者:行者123 更新时间:2023-12-02 06:52:56 25 4
gpt4 key购买 nike

我尝试为工作应用程序(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">

如何消除异常?

最佳答案

好的,我在其他地方找到了帮助。解决方案:因为我的端点扩展了“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);
}

一切正常。

关于spring - 通过 junit 测试 SOAP 端点会抛出 SAXParseException (soapenv :Envelope),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1687762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com