How to avoid code duplication when testing API - java

My question is related to test automation. Moving from postman to intellij with rest-assured, but I'm new to it.
Question: What is the best practice to store requests (request bodies?) in test automation projects?
Additional info: Our service returns prices for various main services, additional services, countries and so on. One way - I could store all of the requests separately, but I think it's not good because with time it will become difficult to maintain. Currently I am using postman where I have over 700 requests. Most of those requests share at least 60% of the request body. I wonder if there is a smart way to create some sort of a template method that I can call whenever I need to create a request and add different input parameters that will have impact on the response.
Thank you.

Took me a while to figure this out, but hopefully it will help someone.
To answer the question "what's the best way to store requests (request bodies)..."
depends on the project. our developers using data transfer objects (DTO) with builder pattern. So, I am also using builder pattern.
create a class for request template and fill it with default values
(in my example I added jar file with classes from dev's project to
my test project so I refer I can refer to them)
create a class for request builders
go on with tests!
instead of duplicating request for each test you simply build the request and call builder to override default values for those you need for test.
Hope this helps :)

Related

How to integrate the API and UI framework in Selenium using JAVA?

API - RestAssured API
UI - Selenium UI
Integration needed from API to UI
using Selenium and Maven dependencies in JAVA
How to call the UI framework from API ?
From the comments section here is my understanding of your requirement :
You want to use rest assured to call an API and create data and then verify the same whether it is visible or present on UI using selenium scripts.
Now, I don't know the exact functionality you want to test but here are my 2 cents.
First of all, what you are trying to achieve from your test is that whether the front and code and the back end code honour the contract between each other or not or in other words whether they are integrated correctly or not.
For example: From the browser, you try to create the record, for which browser may be making API calls then display the same data, one of the ways the test case can fail is that if there is any mismatch in the response objects data type and the data type your front end code is expecting.
Other failure points are on the API level - maybe service is down or the code logic itself is wrong, and UI level - front end code is itself not able to load the record because of UI code logic issue or browser-specific issue.
Now, if we follow your approach, then in cucumber I will write my steps like this :
GivenICallCreateRecordAPICall
WhenICheckTheDataFromUI
ThenItShouldMatchCorrectly
Now, the first step will implement the rest assured call to create the record in the same environment and saving data to be verified with UI.
Second, Call UI using selenium, extract and save the data to be verified,
And Last, assert all the data to verify and throw errors if otherwise.
But, this approach is wrong.
All layers of test should ideally be independent as this will cause flakiness.
The objective here is to check whether the integration is correct or not, so write a contract test Or, in your API test cases verify the response schema and data type.
Still, if you want to know more detail on how to implement then, please let me know I will provide the implementation detail.

Would testing my Java Servlet be neccessary?

I just wanted to ask if it is necessary to test the java servlet that calls another java method (in another file I might add) to do the bulk of the work.
Like for instance, my java servlet currently only is being used to get parameters and displaying the results of the method that was called in html format. So I am only planning on testing my main java file that does all the work!
I am not very familiar with testing so if there would be specific ways to test this simple Java servlet, please let me know!
Testing your Java Servlet or for that case, any Java code is very necessary. The way we always tend to write any code is called the HAPPY PATH, which sometimes overlooks possible scenarios that might break your service. It is always a best practice to code via the TDD way (test driven).
You can Unit Test your Servlet alone, if it does perform the main logic as a single unit.
You can perform Integration testing if your Servlet along with other Java code performs the logic as a whole.
There is this question on SO which covers almost all the details on how to perform this and also the various choices to choose from to perform them.
Hope this helps!
Like for instance, my java servlet currently only is being used to get
parameters and displaying the result of the method is has called in
html format. So I am only planning on testing my main java file that
does all the work!
Ideally, you should Why so?
If something messy happens and servlet broke befoe calling the Main business logic
Testing how parameters are fetched so you can be sure which kind of parameters my Servlet can handle (I personally don't like junk values to be passed to my business logic).

Automatically generate Gatling scenarios

We currently maintain our Gatling simulations manually if a list of REST calls of a page changes. If a developer extends a page with a new call, he has to remember to add that call to Gatling simulation.
How to make sure a simulation is in synch with a web page implementation?
My approach would be to write a Selenium based test and get from a WebDriver a HAR files then parse it to generate a Gatling scenarios. Though possible, this approach feels awkward and I ask myself if there is an established way of solving the problem?
I would believe that the best way to do this is to have a custom Java Class writing the scala files for gatling.
Should take the url and parameters as input and produce a new scala file or just append to an existing scenario.
That should be the best way to do it.
But still it will be better if you directly use the recorder.sh that comes with the gatling setup.
To make sure a developer doesn't forget keeping Gatling simulation in synch with web page implementation, you need to ask him if he executed the simulation as a part of his responsibility to test his own changes.
If this is not how your team works maybe a Quality Assistance is needed?

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 to Accelerate Java Web Testing

I am coding an intricate method in a Spring Controller that takes as input request.getParameterMap(). In developing the method iteratively, each time I make a tweak, I have to Deploy, and then go through the steps on the web form.
That process can take minutes, just to tweak a small code change.
Are there any tricks or methods to speed up this process? All I really need is the input from request.getParameterMap(). Can I serialize that Map data somehow, and re-use it?
I am using Netbeans, if that is relevant.
In my experience the best is to setup a JUnit test, which doesn't use the web server at all, but just instantiates the controller, calls the method and checks the result.
Since your controller wasn't written from the ground up for this kind of approach, it might be quite some work to get this going at this stage. If you post the method in question we might help with this.
The next best thing is setting up an integration test, which starts up the application server, executes the request (possibly through the actual web gui using selenium or something).
Still a lot of work, but the difficulties are less dependent on the current workstyle.
As a final work around you can try to make the roundtrip for a manual test faster. There might be IDE dependent possibilities so you would have to let us know about the IDE in use.
I haven't tested it, but many people praise JRebel for this kind of thing, so you might want to give it a try.
If you don't want to fill the web form again and again try Jmeter(It's a free load testing tool).
Create a test plan with -> set number of threads to 1 --> http request sampler -> set method to post and add post parameters. Once everthing is setup fire the request
please check this link below for reference
http://community.blazemeter.com/knowledgebase/articles/65142-the-new-http-sampler-in-jmeter-2-6

Categories

Resources