For Selenium usually Page Object Model is used. What are good design patterns for REST Assured to organize structure, files?
-
To be clear, Page Object pattern is just one of design patterns used when testing Web UI. There are also other patterns specific for testing Web UIs like Screenplay (infoq.com/articles/…) addressing limitations (sqa.stackexchange.com/questions/27488/…) of Page Objects.– dzieciouJun 11, 2019 at 14:18
2 Answers
Here's a number of design patterns you can use for testing REST services (with REST-assured, but not necessarily*):
- Service Object pattern. Just like Page Object encapsulates HTML page, Service Object encapsulates REST API. Hence, instead of dealing directly with forming HTTP requests and parsing HTTP responses, you can use business level methods like
LoginService.login()
that will handle translation for yourself. Retrofit provides great implementation of this pattern, although you can encapsulate REST-assured calls into Service Objects as well. - Scenario Object pattern. If you have multiple services often used together, e.g.,
LoginAsAdminService
,CreateUserService
andGrantPermissionsService
you can chain them together intoCreateUserWithPermissionsScenario
. - Service Mock Object is a generalization of Mock Object pattern to service-oriented systems. WireMock and MockServer are notable implementations of it in Java world.
You can also incorporate general design patterns, e.g.:
- Decorator patterns to add injecting of authorization token to each of your HTTP requests
- Template method pattern to have parent abstract service that provides authentication mechanism with abstract
send()
method that specific implementation of this service (classes inheriting from it) implement.
I recommend you reading "Design Patterns: Elements of Reusable Object-Oriented Software" book to learn more about design patterns.
*REST-assured is just one of many HTTP clients on the market, so what I presented are examples for design patterns you can use for testing REST API with any HTTP client.
That depend from the size of the project, with a large code base with multiple modules it might be appropriate to create a folder Service and then a class for each module like you will normally do with tests for the UI, but for a small project even a single Class(eg called RestApi) can be sufficient, so basically the same logic of POM still apply for structure and organisation of files the main difference is that rather than test the application from UI(clicking buttons that call API) you test directly the API.