How to simulate a class using WireMock? - java

I have a class AMSClient, which I want to simulate using WireMock. I have seen some examples but I couldn't understand them, as they are using port numbers. Can any one tell me how to simulate a class using WireMock.

WireMock isn't an object mocking tool. If you're looking to mock a class or interface I'd suggest looking at Mockito.
However, if the class/interface in question wraps HTTP calls to another service, then WireMock can help by mimicking the HTTP responses from the service.

Using power mockito concept, I solved this issue
https://examples.javacodegeeks.com/core-java/mockito/powermock-mockito-integration-example/

Related

how to write test case for rest service in scala akka(spray)

How to mock HttpRespose? I am using scalla, akka with spray for calling rest service which respond with json, I need to write test case for same. suggest me the technology that can be used.
The best option would be to use Spray Test Kit
http://spray.io/documentation/1.1.2/spray-testkit/ which comes with predefined methods which allow for some mocking.
If you need to mock an underlaying library Mockito is a grat choice but there are others like Scalamock you could give a try.
For JSON checking I had good experience with using spray's json library and checking on the deserialized object.
Mockito is still for me the best mock-utility (see http://site.mockito.org/). It's written for and in java, but I find it works very well with scala. Another good one is easymock (http://easymock.org/).
You should also evaluate whether you need to go so low as mocking HttpResponse or if you can let your tests operate on a higher level than that.
I try to answer to the title:
how to write test case for rest service in scala akka(spray)
It looks like there are missing points in the detailed description of the question...
It worth to mention that there are different levels of tests we usually write and I would suggest the following:
for unit tests (no actor specific) mocking there many options that not different from usual suspects we have in java (Mockito, EasyMock, JMock) + some scala specific (like ScalaMock). Scalatest provides a sugar wrapper for some of them so I would recommend it.
on integration tests level I would mention akka-testkit which is the best option to verify actors behaviour by exchanging messages. Also as mentioned in previous answer there is spray-testkit which makes testing of route logic easy.
for service/component level tests (send http request and verify the http response) you can use any usual testing library like Scalatest or use more complicated one for writing business oriented tests like cucumber. There are also some options to mock/stub dependencies like: mock-server, wiremock, stubby4j which actually can be used in any language but the question mentions scala so jvm running options should be preferable. From my experience mock-server is the most flexible option here.

How to validate API in tests with Swagger?

I'm trying to figure out the best way to have my API documentation be the source of truth and use it to validate the actual Java REST code ideally through integration testing or something of that sort. We're using the contract first or consumer contract type of approach, so we don't want the documentation to be generated from annotated code necessarily and updating every time a developer makes a change.
One thought has been to use Swagger, but I'm not sure how best to make it be used for validating the API. Ideally, it'd be good to have the validation occur in the build or integration testing process to see if the real response (and request if possible) match what's expected. I know there are a lot of uses and tools for Swagger and just trying to wrap my head around it. Or if there is a better alternative to work with Java code.
Recently, we (swagger-codegen community) start adding automatic test case generation to API clients (C#, PHP, Ruby). We've not added that to Java yet. Here are some example test cases generated by Swagger-Codegen for C#:
https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test
It's still very preliminary and we would like to hear feedback from you to see if that's what you're looking for.
I think you should try swagger-request-validator:
https://bitbucket.org/atlassian/swagger-request-validator
Here are some examples how to use it:
https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-examples/
Another alternative is assertj-swagger:
https://github.com/RobWin/assertj-swagger
You may want to look at Spring Cloud Contract. It offers you a DSL, where you can describe the scenarios (more or less what is the response I get for a given request) and it seems to fit well to what you described as a requirement...
If you're using the Spring Framework, I'd highly recommend checking out Spring RestDocs which allow you to generate

How do you custom configure RESTeasy for use with Mockito?

I'm trying to figure out how to configure RESTeasy for use with Mockito. I had to create custom Serializers and Deserializers. I found this article that talked about creating custom serializers and deserializers and turning them into a module.
I also found this article and this article about configuring Jackson to use the new custom module that I wrote.
The problem that I'm having is that I don't know how to test it. One of the articles says
The answer is to have Jackson(Jaxb)JsonProvider in the resteasy.providers section of resteasy-jaxrs.war/WEB-INF/web.xml
I'm using Mockito to test everything. As far as I know, there isn't a resteasy-jaxrs.war file that is made. Is there anyway that I can fake this?
I should probably also add that I'm using spring and I have an applicationContext.xml file that I use just for creating fake beans etc. for testing.
If you want unit tests REST application go with REST-assured. It is very simple and powerful library - you don't need to mock everything. For other options see Ways to test RESTful services?.
Remember that even if you manage to mock everything, such test might be hardly useful - you'll test your mocks, not a true application.

How to test SOAP Services?

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.

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