Queries regarding RestAssured - java

Recently I've made use of RestAssured for testing Rest API and it seems to be pretty useful.
However I've a query regarding testing rest end-points. We don't deploy our services locally onto a server.
We write the unit test and integration tests and test it and deploy it on a separate dev1 environment.
What I want is to write an integration test that will post a request and test the rest-endpoint using restassured.Kindly advise. Thanks.
P.S. We don't have local server where we can deploy and hit the rest end-point.

Local or remote server doesn't matter I hope. Pls take a look at docs https://github.com/rest-assured/rest-assured/wiki/Usage

You can set the remote server URL globally for all tests using RestAssured.baseURI="<BaseURL>" or you can pass the full URL in RestAssured method itself like get("http://baseURL/service1")

Related

Framework for Integration Test in a Microservice [duplicate]

I am exploring Karate API double (mocking) for the integration test. For the below scenarios, I'm not getting the expected mocking response. Your help will be appreciated.
My Setup :
1. Karate Mock Server up with pathMatches rules on port 8001: http://localhost:8001 ( working, validated against "/cat" and some test calls)
2. My own Application is up from docker on port 8080. From Docker exposed 8001 port as well.
Mocking Case:
1. My application REST call exposed to all users http://localhost:8080/service/v1/findUser. This exposed API, underlying calling other REST call http://dev-STG/userservice/v1/findUser which actually giving JSON response. So, I want to mock underlying API call and validate my API behavior accordingly.
Steps tried:
1. Now, in my application config, m replacing actual underlying API call to Karate mock server(http://localhost:8001/userservice/v1/findUser). Then did build & up my application docker.
In Karate, I defined test e.g "testIntgrtn.feature" which calling my application API "http://localhost:8080/service/v1/findUser" and Karate mock server up and set with pathmatch "/userservice/v1/findUser".
After executing "testIntgrtn.feature" karate not mocking for an underlying call(http://localhost:8001/userservice/v1/findUser).
Now, in "testIntgrtn.feature" file I changed my-application URL to underlying REST URL i.e (http://localhost:8001/userservice/v1/findUser) then mocking will work like charm.
I'm not understanding why underlying API call not getting mocked here? Did I miss something here?
Also, in Karate can we monitor all REST calls (like cypress mocking).
Thanks for this wonderful framework. Which is intuitive for writing automation cases.
Karate cannot automatically intercept calls.
The recommended approach is when you boot the application running at localhost:8080 you change the configuration so that instead of calling http://dev-stg/userservice/v1/findUser it calls something like http://localhost:8001/v1/findUser. This is what most teams do, and is easy because you should anyway be defining external URL-s as application.properties (or equivalent) as a best-practice.
It is very easy to over-ride an application property in Spring Boot for example, you can do this via the command-line: https://stackoverflow.com/a/37053004/143475
If you want, you can dynamically provision a port for the mock. So your unit test can first start a mock, get the port, and then start the server. You can find details in the Karate documentation.
All this said, if you are able to change the (system) HTTP proxy before the app at localhost:8080 starts, you may be able to do this without modifying the configuration. (But it is tricky, so I recommend the approach explained above.) So in this case, Karate can actually "intercept" the outgoing HTTP calls that the app at localhost:8080 makes.
See the second-last row (5a) in the table here: https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example

Build a test that consumes a Spring based REST API URL running on localhost for testing

I have build a REST API Based SMS gateway on Spring boot, along with an extensive testing suite comprising of unit and integration tests for testing the business logic and various layers of the API's architecture. I am now required to create a test which consumes the API call that runs from my localhost under various conditions. My queries are as follows:
Do I need to separately run the Spring Boot application on localhost before I can run the tests that directly consume the API from localhost?
I need to integrate my tests with Travis for Continuous integration. Is it possible to build an integration test which, on being run, starts the Spring application on localhost and directly calls the API URL from localhost for testing its response when different parameters are passed to the URL?
Following from Yogi's comment above, I used RestAssured to build a test suite that directly consumed the API end point running on my localhost.
The following bookmarks helped me understand RestAssured:
For getting a general understanding of how Rest Assured can be used:
https://semaphoreci.com/community/tutorials/testing-rest-endpoints-using-rest-assured
The official RestAssured wiki for understanding the API: https://github.com/rest-assured/rest-assured/wiki/Usage#getting-response-data
An example of using RestAssured via Gradle in a Spring project: https://www.blazemeter.com/blog/how-to-extract-values-when-api-testing-with-rest-assured
Using Basic Authentication with Rest Assured: https://www.youtube.com/watch?v=PAyGma2OMFo

How to debug a dynamic web project with post request in eclipse juno

I can debug easily with a Get request (Debug on Server, Tomcat is started and I can proceed easily) as I just need to pass parameter in url but I am not sure how to debug with a post request.
Currently I am using Advanced Rest Client to run the application and checking logs to find any error.
Any help is appreciated.
Postman is very useful chrome extension for that.
Start the server in debug mode, then send post request via Postman. You can also use any other http methods.
I added a screenshot that shows how to send a post request to local machine with json parameters.
Set-up and start Tomcat from Eclispe and set a break point in your POST method.
Then use a client linke curl, wget or SOAP UI.
Or even bether: Write integration tests which start tomcat and use soemthing like the Apache HttpClient. You could write a JUnit class rule which starts and stops tomcat for your tests.
This is more initial work, but gives you infinitely repeatable test for continous integration.

Mock a physical web server while junit test running

I am searching for a simple framework which is able to mock JSON responses for specific URLs and therefore starts an in-memory server. E.g. the framework could start a node.js server or similar.
I need a physical server since the unit tests are running against an external application (using selenium) and this application has a dependency to a JSON interface. The application itself is iPhone-App running inside of simulator and communicates to a REST interface.
Is there an existing framework for this or what would be the best approach given that I need to execute the tests with jUnit.
You can start a stub server, given that you can configure the JSON URL in your client application.
Have a look for example at:
https://github.com/dreamhead/moco
https://stubby4j.com
You can start these in your maven build during the pre-integrationtest phase and shut them down in post-integration test phase.
You can use npm package stubby-db. What you need to do is;
Install : npm install stubby-db -g
Mapping : Create a request response mapping which is quite easy. Specify the path of response file having JSON response.
Run the stub server: stubbydb
That's all. However since you just want to run it for your unit tests only which doesn't require much performance, you can also use wiremock. It starts internally, and you never feel like you are running an external application.

How can I create Endpoint for WebService?

How can I create an Endpoint for a web service? I am a beginner to the web services world.
I have got the WSDL and I would like to create a web service based on that WSDL. I have used Apache CXF to generate client stubs. What would be the next steps to test it as a service?
How can I create EndPoints?
How can I mimic the WSDL soapbind address locally and test it?
Let me clarify the Question. Looks like there is confusion. Thanks #Buhake Sindi for point it out.
I have got the WSDL and generated the Client stubs by using Apache CXF Framework in Eclipse. I need to test the WebService client code whether its working or not. How to test this approach now? My WSDL URL is not working at this moment.
How to test my client stub(from generated Impl Class)?
Do I need to create any Endpoints to mimic the WSDL URL(which is not running now)?
Hope the Question is clear now...
You can test client code even without creating web service implementation also. Are you aware of SoapUI tool? Use that to import the wsdl to create a project.
It gives you the option to create a mock service also along with the request. You can run that mock service and test your client against that service without writing any service code. I use it for testing all the time. Also you can create Success, Failure, and Fault response to test different scenarios. Mock service will also show you the request received by the service. This feature works like a charm. Let me know if you need help in setting up mock service in SoapUI.
You may follow this link to get started:
http://www.soapui.org/Getting-Started/mock-services.html
You run wsdl2java from CXF to generate server-side stubs. Then you fill in the code in the stubs. Then you set up a service to deploy.
There is no mechanism I know if that will create mock services. What would you expect them to do?
See the samples from the CXF distribution, particularly the wsdl-first samples.
A better approach would be to use JMock to mock the client side SEI, and not try to come up with a dummy service.

Categories

Resources