Enterprise Java

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.

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button