0

My application consumes external third-party webservices (Im successfully using cxf for this). How can I mock this webservices using local files to build pre-saved reponses (for test purposes) ?

More specifically:

I was thinking of using 2 maven projects: dao-ws and dao-ws-mock, both having the same interface.

The first dao-ws really calls the webservices using cxf, whereas the second dao-ws-mock uses local files to build pre-saved responses (used for test purposes).

mvn install build the webapp project, whereas mvn install -DuseMock build the webapp project with the dao-ws-mock dependency. Is this the correct way to do it? Is there a better/simpler way to do it?

Depending of the properties used, I will produce the same .war, but with different behavior. It sounds to be a bad practice for me (for example, I don't want to push war with mock dependencies on our internal Nexus). What do you think?

Best regards,

Nicolas C
  • 1,584
  • 2
  • 17
  • 33
  • Lemme get this straight.. You're wanting to test your application which will be deployed as a war and you're entertaining the idea of creating a different war that you will do integration testing with? Something seems fishy to me with that. Test the war you will deploy not one that you've ginned up so you can write some tests--guess it could work in a pinch, but doesn't seem wholly valid to me. – unigeek Apr 01 '14 at 14:26
  • No exactly: the third-party webservice is not always consistent (In my case, the amount of data returned can change, so the result of the tests will change). I need to mock the webservices to ensure that the data returned by the third-party will always be the same for my tests. – Nicolas C Apr 01 '14 at 15:10
  • Personally, I'm not keen to write integration tests, which it still seems to me is what you're describing. Can you not instead just write unit tests where you mock out that web service that you depend on and just test your class/classes where you use that web service? If you did this, your test code is easily not deployed with your war. Your unit tests can easily read whatever file you want to substitute for an actual web service result. I like Mockito or JMockit--are you familiar with either? – unigeek Apr 01 '14 at 15:18

2 Answers2

1

You could use SoapUI's build in mock services - http://www.soapui.org/Getting-Started/mock-services.html

You can generate a mock service based on a wsdl, specify default responses, and you can even create dynamic responses that return different responses depending on the request.

You can then build your mock services into a .war and deploy them: http://www.soapui.org/Service-Mocking/deploying-mock-services-as-war-files.html (This link shows how to do it in the GUI, but it can be done using maven as well)

Matthew Wilson
  • 2,045
  • 14
  • 27
  • I've seen SoapUI before, but the problem is that the mock server will be an independant server, that I need to launch before my tests. How can I launch such a mock-server with the classic ``mvn test`` ? – Nicolas C Apr 01 '14 at 15:13
  • Have a look at the maven lifecycle (http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference). You can run different goals at different phases. Here's another question that explains how to use them - http://stackoverflow.com/questions/14771668/running-code-before-and-after-all-tests-in-a-surefire-execution – Matthew Wilson Apr 02 '14 at 12:43
  • Perhaps you are looking for mvn integration-test – unigeek Apr 02 '14 at 15:09
-1

You could use Sandbox - mock services are hosted and always available so there is no need to launch another server before running tests (disclaimer: I'm a founder).

You can generate mocks from service specifications (wsdl, Apiary, Swagger) and add dynamic behaviour as needed.

drew
  • 41
  • 1
  • 3