Groovy/Grails mock web service based on WSDL - grails

I've inherited a Grails app which makes calls out to a web service, using javax.xml.ws* classes and I'm trying to find a way to mock the web service based on the WSDL for the integration tests. I realize that I can use one of the java soap implementations to build this, but I'd rather stay in groovy.
So my question is, is there an idiomatic groovy way to build a web service based on a WSDL?

One approach that is pretty straightforward is to use Jetty to create a mock. It´s easy to create a mock that looks at the request and generates a response, especially if you´re using Groovy. For instance, create a template response file and use the XmlSlurper to fill in values. Then you can either start the mock in your tests or run it independently.
I found the following blog that roughly explains the basic concept:
http://olafsblog.sysbsb.de/lightweight-testing-of-webservice-http-clients-with-junit-and-jetty/

Related

What's the proper way of integration testing libraries that run injected code?

I have two software components: My application and a library (which is owned by my company, but another team), which is used by the application. The library is a client library for some service and performs HTTP requests. The library also maps the HTTP response to the internal representation of the application. This is done by injecting a mapping class into the library by the application.
I already have unit tests for the mapping class and for the application, whereas the client library call is always mocked.
Now I'm thinking of integration test the library and I'm not sure what's the best way to do that:
Mock the library call and only check that it's called with the correct parameters
Pro: If the internals of the library change (with a non-breaking change) I don't have to adapt my tests.
Con: The mapping class isn't integration tested. I can't be sure that the library is correctly configured or that the parameters the mapper gets from the library are what I expect them to be.
Mock only the HTTP call done by the library
Pro: The mapping class and the configuration of the library (if I configured it correctly) are tested.
Con: I need to figure out the internals of the library and check how the HTTP call of each test case will look like. Also if the library upgrades to a new version of the service, I would need to adapt all the HTTP mocks as well and actually shouldn't care about how the library internally works.
Replace the HTTP call in the library with an in-memory fake (=dummy) implementation during testing
Pro: Everything is tested + the tests are resilient against library changes.
Con: It's an effort to implement and maintain a fake implementation. Depending on the service, this could mean rebuilding the functionalities of the service in the library. Who should be responsible (implementation + maintenance) for the fake strategy? My team or the team that owns the library?
I'm in favor of the last point, but given that the internals of our library rarely changes, I'm not sure if a fake strategy is worth the effort.
What's your opinion on this? Can you think of another solution?
I would create helpers inside the library that would allow you to mock the HTTP response. Therefore, you'd see the code running inside the library and you could use the libraries that verify the JSON format to make sure that the http request/response is the one you're expecting.
In this sense, you're checking i) the library actually works with your system; ii) the processes the correct HTTP response; thus, your helper could be easy enough so that developers would only need to provide the content of the http response

RestAssured vs Mockmvc for unit and integration testing

I am asked to choose one and the best from these two for unit and integration tests-
1. RestAssured
2. Mockmvc
The application is in Spring and Spring Boot. I have read many blogs and no clear distinction is given or I couldn't find one. For rest assured, its neat and clean code and BDD style that makes it more readable. But doesn't sound a convincing point. Some of the tests are written using Mockmvc and I am trying to write the same in RestAssured to compare the same.
I know this may sound a theoretical question but I am unable to get points in favor of one and suggest which one is better and why. Looks like a choice of flavor to me. Any views and suggestions?
You would choose MockMvc for web layer testing. These tests will allow you to validate your controller classes properly handle respective HTTP requests. Since these are practically fine grained controller unit tests, you can additionally include them as part of your apps’ code coverage percentage with tools like JaCoCo. On a side note, these tests will likely run faster than integration tests since they won’t require the web server to run on.
RestAssured is used for integration testing in your Spring Boot app. When it comes to RESTful based API integration testing and validation, RestAssured offers convenient methods to create and execute your HTTP calls with custom headers, auth, content types, query parameters, payload content, cookies, etc.
To aid your comparison check out this article - Testing Spring Boot RESTful APIs using MockMvc/Mockito, Test RestTemplate and RestAssured - it has a good explanation and robust examples on the usage for RestAssured and MockMvc.

Using spring-ws with already existing WSDL

I am new to spring-ws. what ever tutorial i see starts with xsd and at the end generates a wsdl. What is the approach when we have already an existing wsdl.
Also i was having a doubt on contract first approach which is already discussed (though am not getting convinced with the answer)
spring-ws and contract-first approach
My assignment is to use spring-ws with an existing wsdl. can you please provide me an approach for this.
As per my understanding. In the process of contract first approach, I got the contract already so how to proceed further is not shown in any tutorial.
When developing a web service using Java you can use one of two approaches:
Contract-first: start with an WSDL that defines the web service operations and their input/output messages. Then generate the corresponding Java objects to implement the service.
Contract last: Start with the implementation of one or more methods in Java and generate the WSDL files based on these methods and the Java objects that they use.
Spring-WS, as you already mentioned, only supports the contract first approach. This means that you cannot develop a web service using Spring-WS without first having a WSDL or XSD that describes the input/output messages.
You should be able to create a service using Spring-WS using any valid WSDL file. For a concrete example let me point you to a blog post that I created that illustrates how you can develop a web service using Spring-WS starting from a WSDL file.
Actually spring does support both static and dynamic wsdl. But each comes with different challenges. As from what i have seen, spring works on a notion of pattern matching when comes to generating a wsdl dynamically from a xsd. Like "Request" string which says input and "Response" means output. Now here is a problem where the spring generates a wsdl with synchronous responses. If our requirement is to have asynchronous response then the dynamic wsdl wont work.
To overcome this, we can use the static wsdl and let spring know not to generate wsdl dynamically.

Execute Grails Filters in Integration Tests

I was through the integration testing documentation for Grails and I noticed this line:
Grails does not invoke interceptors or servlet filters when calling actions during integration testing.
source: http://grails.org/doc/latest/guide/testing.html#integrationTesting
Why is this? It would make my testing life a lot easier if Grails did invoke the filters. My project makes heavy use of filters and many of my controllers depend on my filters in order to do anything.
I was thinking about it and it seems like one could use groovy black magic to automatically execute the filters in an integration test. Has anyone already done this, or is this something that I'd have to write?
The environment used for integration tests is similar to what's available during run-app; Spring is active, plugins are loaded, a database is available, etc. Pretty much everything except for a web server. Without a server, there are no real requests, no servlet filters, and no Grails filters (which are wrappers for Spring controller HandlerAdaptors). When testing controllers you can access a request and response thanks to the Spring servlet API mock classes. But none of the real web request lifecycle is active, it's all just simulated.
You're right that it should be doable with some custom code. When you do this, please consider making it a plugin so we can all share :)

Selenium mock server access

I have a website that I want to test using Selenium WebDriver (version 2.28), and to integrate it with my CI (Maven and Hudson).
The thing is that in order to properly test the front-end, I need to access the server (for the actual HTML and for REST resources). The test will not be fully encapsulated if I access the actual server that I build and deploy during the build process, since the data might be different each time (based on the data that resides in the DB of the built environment at the time of the build).
Therefore it seems to me that I have to mock the server access.
I just don't find a support for this in Selenium, and I can't quite think of the best way to do that without a Selenium support.
I can create mocks to all of my resources (HTML and REST) in the actual server, on different URLs, and access those in my tests instead of the production-level ones (with some kind of a flag to indicate that I should access mock resources instead of real ones). But that's not really mocking...
For reference, I saw that in the Sahi test framework there's a feature of addMock(url, class_function) - in which when Sahi runs into the specified URL, it will call the specified class and function instead of access the URL, whereas the class and function should provide the resource instead of the URL providing the resource.
I'm looking for something similar in Selenium (though perhaps it's not possible, since Sahi functions as a proxy).
Many thanks in advance,
Daniel

Resources