jeudi 2 juin 2016

Set up and use simple web service in tests

I have simple web service class as below:

@WebService
public class Param {

    @WebMethod
    public Dictionary getDict(@WebParam(name = "dictname") @XmlElement(required = true) String dictname) {
        //do some stuff
        return dict;
    }
}

I have wsdl file of it as well. I used to call this service on remote server (from other server) by calling classes generated by CXF plugin from wsdl file. It looked like below:

ParamService paramServices = new ParamService(new URL(getParamWsdlUrl()));
Param param = paramServices.getParamPort();
BindingProvider bindingProvider = (BindingProvider) param;
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, getParamUrl());
Dictionary dic = param.getDict("test");

But now I would like to test this web service in junit. I would like not to use remote server for this task but to setup it locally using jetty or some similar mechanism. The problem I encounter here is that if I want to create jetty instance hosting my service (1) I want Param class to be my own implementation. Unfortunately later (2) I need to use Param class generated by CXF. The problem is that both class have exactly the same names and packages so compiler does not know which one to use.

Endpoint endpoint = Endpoint.publish(getParamUrl(), Param.class); // 1

ParamService paramServices = new ParamService(new URL(getParamWsdlUrl()));
Param param = paramServices.getParamPort(); // 2
BindingProvider bindingProvider = (BindingProvider) param;
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, getParamUrl());

Dictionary dic = param.getDict("test");

So here is my question. How can I fix this or how can I change this to test this service? Maybe there is some simpler way to call web service set up by jetty? I would like to omit using service as stream and sending there xml files because it seems to my really complicated and uncomfortable.

Aucun commentaire:

Enregistrer un commentaire