How to test SOAP Services? - java

How do you guys Test your SOAP Services? Do you use Tools like soapUI or do you write Unit Tests?
Just wanted to hear some opinions, what you prefer, what the advantages or disadvantages of both approaches are? And if someone writes Unit Tests, can you give me an example how to write those???
Edit: I developed a lot of REST services, which I usually tested using JUnit and a REST Client Framework. So when the REST Service was deployed, I was able to invoke those services with as a JUnit Test using a http connection. Is there something similiar in SOAP too? Does anyone have an example code for a SOAP Client?

The best way to test your SOAP service is by using the SOAPUI testing tool.
With JDEF you can create your SOAP Application, following SOAP standards - and then easily verify this through the Enterprise Manager Console provided by Oracle.
You just get an instance of that particular service, and then you can see the audit flow.

I use both Junit for functional low-level testing of my functions and back end code. And I use SOAPUI to test the Web Service. Also keep in mind that you can run SOAPUI tests from within unit tests as desribed here. example:
public void testRunner() throws Exception
{
SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
runner.setProjectFile( "src/dist/sample-soapui-project.xml" );
runner.run();
}

If you like to test services you can use SOAPUI tool which is easy. But if you like to test service's functions are wroking right or not, you need to right unitest. That means if you are the author of the webservice, you might need to write unittests to check the functionalities.

I'm using both. Basically JUnit for simple automated unit tests. And SoapUI for more complex system tests which make more that a single web service call.

I've written a small library which does most of the heavy lifting of unit-testing SOAP services. Basically you'll get mockito mocks which are easy to work with.

Related

void method in Java Integration Tests

I have some experience with Unit Tests in Java and now started to write Integration Tests. However, I have some troubles understanding the Integration Test and writing test. Here are some issues that I would like to be clarified:
1. In my Java (based on Spring Boot) project, should I write Integration Test for Controllers or may it be also ok to write Integration Test for Services also (because there are some methods that are not called from Controller).
2. How can I test a void in service by Integration Test? I have not found a proper example on the web and I thought there is no need or way to test void via Integration Test. Any clarification pls?
OK, so here are the best practices in the area.
should I write Integration Test for Controllers or may it be also ok to write Integration Test for Services also
Most of the integration tests run for services. This is because each service can support several controllers and multiple interfaces in general. For controllers you do just the happy path tests on an integration level.
Any tests for controller specific exceptions go into the controller unit test, where you mock out the service.
How can I test a void in service by Integration Test?
You check its side effect. For example, you use a repository class to see if the relevant data has been persisted to the database.
Does this solve your problem ? Let me know in the comments.

How to access code of JAVA WS app.(Windows)

I am wondering if there is a way to access the code of the Java WS application.
I would like to find an alternative method of application automation (directly calling procedures and passing parameters) instead of using some external UI automation software.
Does anybody have any thought? or this is a bad idea to go this way?
Well, Server-Side code, e.g. WebService/Servlets, etc, are meant to be deployed to a web container, and accessed and tested by external applications.
However, usually you can write JUnit tests mocking the application server objects, e.g. HttpServletRequest, HttpSession by libraries like mockito or EasyMock. Mocking can go all the way to simulate values from database, server-parameters, potentially all dependencies of your test code.
Sometimes, you can also write JUnit tests with Java Reflection, to ensure correct values of Annotations, to test something like:
#WebService(name = "myWebService", serviceName = "SomekWebService",
portName = "AnotherWebServicePort", targetNamespace = "http://www.mine.com/ws/hello")
Usually, a project has both simple JUnit tests if possible, but also full integration tests with running on the server, in cases you need to get actual values.

Is mocking an essential part of unit testing?

I have a web service which calls a third party web service.
Now I want to unit-test my web service. For this, should I mock the third party web service or is it fine to call it during the tests?
Is there any standard document on unit testing?
Yes, you should mock the third party web service for unit testing. Here are some advantages:
Your unit tests are independent of other components and really only test the unit and not also the web service. If you include the service in your unit tests and they fail, you won't know whether the problem is in your code or in the foreign web service.
Your tests are independent of the environment. If the Internet connection is ever down or you want to test on a machine that doesn't have Internet access at all, your test will still work.
Your tests will be much faster without actually connecting to the web service. This might not seem like a big thing but if you have many many tests (which you should), it can really get annoying.
You can also test the robustness of your unit by having the mock send unexpected things. This can always happen in the real world and if it does, your web service should react nicely and not just crash. There is no way to test that when using the real web service.
However, there is also integration testing. There, you test the complete setup, with all components put together (integrated). This is where you would use the real web service instead of the mock.
Both kinds of testing are important, but unit testing is typically done earlier and more frequently. It can (and should) be done before all components of the system are created. Integration testing requires a minimal amount of progress in all of most of the components.
This depend on what you're unit-testing.
So if you're testing out whether you can successfully communicate with the third-party webservice, you wouldn't obviously try to mock this. However if you're unit-testing some business use-cases that are part of your web-service offering (unrelated to what the other modules/web services are doing), then you might want to mock the third-party web service.
You should test both but both test cases does not fall under Unit testing.
Unit testing is primarily used for testing individual smaller pieces i.e. classes and methods. Unit testing is something that should preferably happen during the build process without any human intervention. So as part of Unit testing you should mock out the third party webservice. The advantage of mocking is that you can make the mocked object behave in many possible ways and test your classes/methods to make sure they handle all possible cases.
When multiple systems are involved, the test case falls under System/Integration/Functional testing. So as part of your System/Integration/Functional testing, you should call the methods in other webservice from your webservice and make sure everything works as expected.
Mocking is an usually essential in unit testing components which have dependent components. This is so because in unit testing , you are limited to testing that your code works correctly ( does what its contract says it will do ). If this method , in trying to do the part of its contract depends upon other component doing their part correctly , it is perfectly fine to mock that part out ( assuming that they work correctly ).
If you dont mock the other dependent parts , you soon run into problems. First , you cannot be certain what behavior is exhibited by that component. Second , you cannot predict the results of your own test ( because you dont know what was the inputs supplied to your tests )

Writing Tests for Background Processes (like background jobs)

I have a web application built using Spring which contains some jobs.
A typical job is to run through the database, get a list of modified customers, generate a file and FTP it. My question is, how to go about unit testing in this job?
Should I only write unit tests for each "step" of the job, like:
Test for the method which fetches the modified customers.
Test for file generation code.
Test for FTP'ing the code.
But in this case, I will miss the "integration" test case for the above job. Also, Emma reports there is untested code in form of the job.
Any thoughts appreciated.
Thanks!
Unit testing is actually testing only one class at a time. That means you have to mock the dependencies. Spring is great for that.
I would advice Mockito to do the mocking. It is a marvellous tool, and you will learn TDD which is also a way to write beautiful code.
Integration test is another topic and requires another strategy.
Testing against the database is done by extending AbstractTransactionalJUnit4SpringContextTests. You will find examples on the net. In general you also use an in memory db to make those tests (h2 is good for that). It can be done in the unit test phase.
Generating the file can be done as unit test. You generate files and verify the proper content. Or errors...
For the FTP part, I would say it's more part of an integration test, unless you can spawn an FTP server from your build script.
You have to write an unit test for each step. Maybe you'll need to mock some methods.
And then, you can write an integration test to validate the whole, but maybe you'll need to stub some parts (like the FTP server, using an embedded FTP server in your test).

How to mock entire JAX-RPC communication session?

I have a legacy application, which is working with third-party web service through JAX-RPC. Now I need to unit-test the application by mocking certain XML RPC calls with test data. Actually, I need to replace Apache Axis, which is used by the application, by some other library that will be JAX-RPC compliant, but will return what I'm telling it to return. I'm pretty sure I'm not alone with such a problem... Are there any open source libraries for this purpose?
You can do it with Spring framework and EasyMock.
What's the best mock framework for Java?
I have had some success with WireMock. It's a Jetty server that you set up programmatically to respond to certain request patterns with content that you also specify. I have been able to set it up to respond to XML-RPC requests from my class. E.g.,
stubFor(post(urlEqualTo("/RPC2"))
.withRequestBody(containing("<methodName>...</methodName>"))
.willReturn(aResponse()
.withBody("<methodResponse>...</methodResponse>")));
For Mocking the calls to external services you can use EasyMock+Powermock or Mockito
you can do something like this
Easymock.expect(your function calling external Systems).andReturn(your required output)
hope this helps!
Good luck!

Categories

Resources