views:

705

answers:

1

Hi all,

I've try to implement integration tests for a working application (spring, hibernate, soap, cxf). I build an SOAP-XML by hand and handle it to my endpoint. Like this:

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);
}

In my test I handle it to my endpoint like this:

@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", "[email protected]", "[email protected]", "myPassword1"));
    }
}

But when I try to run the test I get an exception "Cannot find the declaration of element '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)

I would guess I have to define the "soapenv" namespace somewhere where I havn't it. But if so, I don't know where.

Start of "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"&gt;

Start of "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"&gt;

How can I get rid of the exception?

+2  A: 

Ok, I've found help elsewhere. The solution: because my endpoint extended "PayloadEndpoint"

public interface RegisterEndpoint extends PayloadEndpoint {

}

I have to insert only the payload into the endpoint (sounds logical ;) ). If I do it like this:

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);
}

all is working.

Arne
Are you sure you want to use string building to make an XML document? What if `password` contains an `<`? At least XML-escape the variables, better yet use a DOM API to build the document.
Tomalak
Yes, you are right. But this is only the code used in the tests, not in production code. I don't care about exceptional input at this place. All I care about here is readability.
Arne