【问题标题】:Testing SOAP endpoint through junit throws SAXParseException (soapenv:Envelope)通过 junit 测试 SOAP 端点抛出 SAXParseException (soapenv:Envelope)
【发布时间】:2009-11-06 13:58:41
【问题描述】:

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

我怎样才能摆脱这个异常?

【问题讨论】:

    标签: spring soap junit xslt cxf


    【解决方案1】:

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

    一切正常。

    【讨论】:

    • 您确定要使用字符串构建来制作 XML 文档吗?如果password 包含&lt; 怎么办?至少对变量进行 XML 转义,最好使用 DOM API 来构建文档。
    • 是的,你是对的。但这只是测试中使用的代码,而不是生产代码。我不在乎这个地方的特殊输入。我在这里只关心可读性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    相关资源
    最近更新 更多