DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Consuming SOAP Service With Apache CXF and Spring
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Microservices Testing: Key Strategies and Tools
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above

Trending

  • Data Contracts as the "Circuit Breaker" for Model Reliability
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • Observability in Spring Boot 4
  • Java Backend Development in the Era of Kubernetes and Docker
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. You Don't Need to Mock Your SOAP Web Service to Test It

You Don't Need to Mock Your SOAP Web Service to Test It

By 
Antonio Goncalves user avatar
Antonio Goncalves
·
Oct. 26, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
53.7K Views

Join the DZone community and get the full member experience.

Join For Free
A short blog about a topic I was discussing last week with a customer: testing SOAP Web Services. If you follow my blog you would know by now that I’m not a fan of unit testing in MOCK environments. Not because I don’t like it or I have religious believes that don’t allow me to use JUnit and Mockito. It’s just because with the work I do (mostly Java EE using application servers) my code runs in a managed environment (i.e. containers) and when I start mocking all the container’s services, it becomes cumbersome and useless. Few months ago I wrote a post about integration testing with Arquillian. But you don’t always need Arquillian to test inside a container because today, most of the containers are light and run in-memory. Think of an in-memory database. An in-memory web container. An in-memory EJB container.

So first, let’s write a SOAP Web Service. I’m using the one I use on my book : a SOAP Web Service that validates a credit card. If you look at the code, there is nothing special about it (the credit card validation algorithm is a dummy one: even numbers are valid, odd are invalid). Let’s start with the interface:

import javax.jws.WebService;

@WebService
public interface Validator {
    public boolean validate(CreditCard creditCard);
}

Then the SOAP Web Service implementation:

@WebService(endpointInterface = "org.agoncal.book.javaee7.chapter21.Validator")
public class CardValidator implements Validator {

  public boolean validate(CreditCard creditCard) {

    Character lastDigit = creditCard.getNumber().charAt(creditCard.getNumber().length() - 1);

    return Integer.parseInt(lastDigit.toString()) % 2 != 0;
  }
}

In this unit test I instantiate the CardValidator class and invoke the validate method. This is acceptable, but what if your SOAP Web Serivce uses Handlers ? What if it overrides mapping with the webservice.xml deployment descriptor ? Uses the WebServiceContext ? In short, what if your SOAP Web Service uses containers’ services ? Unit testing becomes useless. So let’s test your SOAP Web Service inside the container and write an the integration test. For that we can use an in-memory web container. And I’m not just talking about a GlassFish, JBoss or Tomcat, but something as simple as the web container that come with the SUN’s JDK. Sun’s implementation of Java SE 6 includes a light-weight HTTP server API and implementation : com.sun.net.httpserver.

Note that this default HTTP server is in a com.sun package. So this might not be portable depending on the version of your JDK. Instead of using the default HTTP server it is also possible to plug other implementations as long as they provide a Service Provider Implementation (SPI) for example Jetty’s J2se6HttpServerSPI.

So this is how an integration test using an in memory web container can look like:

public class CardValidatorIT {

  @Test
  public void shouldCheckCreditCardValidity() throws MalformedURLException {
    // Publishes the SOAP Web Service
    Endpoint endpoint = Endpoint.publish("http://localhost:8080/cardValidator", new CardValidator());
    assertTrue(endpoint.isPublished());
    assertEquals("http://schemas.xmlsoap.org/wsdl/soap/http", endpoint.getBinding().getBindingID());

    // Data to access the web service
    URL wsdlDocumentLocation = new URL("http://localhost:8080/cardValidator?wsdl");
    String namespaceURI = "http://chapter21.javaee7.book.agoncal.org/";
    String servicePart = "CardValidatorService";
    String portName = "CardValidatorPort";
    QName serviceQN = new QName(namespaceURI, servicePart);
    QName portQN = new QName(namespaceURI, portName);

    // Creates a service instance
    Service service = Service.create(wsdlDocumentLocation, serviceQN);
    Validator cardValidator = service.getPort(portQN, Validator.class);

    // Invokes the web service
    CreditCard creditCard = new CreditCard("12341234", "10/10", 1234, "VISA");

    assertFalse("Credit card should be valid", cardValidator.validate(creditCard));
    creditCard.setNumber("12341233");
    assertTrue("Credit card should not be valid", cardValidator.validate(creditCard));

    // Unpublishes the SOAP Web Service
    endpoint.stop();
    assertFalse(endpoint.isPublished());
  }
}

The Endpoint.publish() method uses by default the light-weight HTTP server implementation that is included in Sun’s Java SE 6. It publishes the SOAP Web Service and starts listening on URL http://localhost:8080/cardValidator. You can even go to http://localhost:8080/cardValidator?wsdl to see the generated WSDL. The integration test looks for the WSDL document, creates a service using the WSDL information, gets the port to the SOAP Web Service and then invokes the validate method. The method Endpoint.stop() stops the publishin of the service and shutsdown the in-memory web server.

Again, you should be careful as this integration test uses the default HTTP server which is in a com.sun package and therefore not portable.

 

SOAP Web Protocols Web Service unit test

Published at DZone with permission of Antonio Goncalves. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Consuming SOAP Service With Apache CXF and Spring
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Microservices Testing: Key Strategies and Tools
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook