Тестирование конечной точки SOAP через junit выдает исключение SAXParseException (soapenv:Envelope)

StackOverflow https://stackoverflow.com/questions/1687762

Вопрос

Я попытался реализовать интеграционные тесты для работающего приложения (spring, hibernate, мыло, 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);
}

все работает.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top