Home » Java » Enterprise Java » Automated testing of REST-services

About Alexey Zvolinskiy

Alexey Zvolinskiy
Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Automated testing of REST-services

Despite I’m a Java and Scala developer I still passioned a lot in testing of software. If to be more precisely – web applications. It’s really interesting to develop web app and be confident that the apps have a good quality.

When I’ve started a career the most popular web architecture was MVC (Model View Control) and there was pretty simple. When you develop some business logic you have to write some unit tests which check a functionality of internal controller functions and this is enough. Integration tests were more complex task in that time because such kind of tests implies usage of some mock frameworks.

But the time go ahead. And the old MVC architecture has become insufficient for numerous client types such as smartphones, tablets, browsers. REST architecture started substitute the MVC. More and more apps started use one API to communicate with different clients via HTTP. This circumstance was more then innovative. Because all business logic related to data and data processing was concentrated on servers meanwhile the client-side was responsible for representation of the data and some extra manipulation with it.
As you probably guessed I just described a principles which are used in a Single Page Application approach (SPA). In this manner built a lot of modern apps Facebook, Instagram, Twitter.

While developers have changed their apps, the ways of testing were mutate too. As a result appeared a new layer on which testing was not just applicable but was very efficient. I talk exactly about API layer. Since an API is consumed by different clients (smartphones, desktops…), make sense to gather a group of tests which check a common logic for all types of clients and to highlight the client-specific test scenarios to focus on a client specific logic. The logic works with data which was already tested in API layer.

Such approach gives us an amazing testing strategy. Testers save time, because they do not need to repeat tests on different clients by interacting with already tested data sets. They just need to pay all attention to a UI and some specific features.

Automated testing of REST-services

In my own experience I perform testing of REST API layer by writing automated test scripts. For this purpose I use REST-assured library developed by JayWay company. This java library is really strong weapon for automated testing of REST-services.

The code of such tests looks really nice:

@Test
	public void getLandLaordTest() {
		given()
			.contentType(ContentType.JSON)
			.pathParam("id", "DoYlvMXN")
		.when()
			.get("/landlords/{id}")
		.then()
			.statusCode(200)
			.body("firstName", equalTo("Sam"))
			.body("trusted", equalTo(false));
	}

It’s pretty concise and not verbose at all. One more advantage of REST-assured usage in a java projects is its simplicity. I can teach any member of my team to develop such tests in a 3-4 hours. Also it works well with the most popular java testing frameworks such as TestNG, JUnit and Hamcrest.

Summary

I recommend to test code you write because it lifts you on a next level of software development competences. Automating of work is a key to success and investment in saved time.

Reference: Automated testing of REST-services from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of