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.
Related
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
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
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")
Is JUnit able to test a websocket server, or is JUnit unable to complete this task and should another tool then be used to test a websocket server using Java? As of note the websocket server is hosted on a page served by Tomcat 7.0.53.
Your question is confusing. However, perhaps you can look at HTMLUnit which is API that allows you to programmatically access webpages. You can use that with JUnit to test your webpage. You may also want to look at JWebUnit that is based on HTMLUnit. Something else that might be helpful is that you can debug your webapp using Eclipse. You can either run the tomcat server in eclipse or connect to a running server instance (You will need to set some options when starting tomcat if you do not run within Eclipse)
Hope this helps.
I'm using JwebUnit to test my JSP pages in a web application.
Since JWebUnit requires a base urlto work. JwebUnit has setBaseUrl("http://localhost:8080/MyWebapp"); to set the base url which is used #Beforeof Junit Test. I need to start a server and deploy the webapp there before each test and stop the server after that. Can anyone please suggest me a simple way to do this with Jetty or other options...
You may have a look at Server Rule. It provides a JUnit rule that starts and stops a Jetty web server.