DEV Community

cuongld2
cuongld2

Posted on • Edited on

Create simple REST API for practice automation testing

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications (services) to talk to each other.

Imagine it like a waiter/waitress in a restaurant. It will help you get your info about your order and transfer it to the cooks in the kitchen. When the food is ready, it will go get the food and bring it to you.

In tech terms, the database is like a kitchen. API is like a waiter. And you(the customer) is like client request.

To create APIs, we can do it in many ways/languages. But for this tutorial I will show you how to do it with Java Spring/Tomcat with mvn.

The database we interact with will be MySQL.

1.Install MySQL and create a new database:

MySQL is an opensource RDMBS so feel free to install and use.
As I'm using Ubuntu, the installation guide you can refer to MySQL installation

After that, you can create your own database, which in this blog post, name as gamelistapi.

Refer to this for how to create a database in MySQL Create a database.

To work with databases, I often use DataGrip ( a development tool created by Jetbrains). DataGrip support a lot of databases : MySQL, Oracle, SQL server, Postgres, MariaDB, etc..

Datagrip official site is [here][https://www.jetbrains.com/datagrip/]

After create your new database, you can open Datagrip to interact with your database via SQL query or by Datagrip GUI.

Create new connection:
Alt Text

Config for connect with gamelist api:

Alt Text

2.Setup Java and Maven Environment:

You will need to install Java (I'm currently using Java 11 but you can go with Java 8 as it is stable version).

Go to oracle site for download the proper java installer for your system: Java oracle

After that install maven tool (used for building java app). Please go to maven download official site for download maven : maven latest version

You will need to setup global environment for java and maven environment before you can use java and maven in your project. (The setup depends on which OS you are using)

3.Project structure:

  • In maven project we will have pom file: to store dependency (library) we will use for our project)

  • In src/resources folder, we usually put in the database, environment, configuration information used for the project. In this project, we put db.properties.

The file db.properties looks like this :


// MySQL properties

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/gamelistapi?useSSL=false
mysql.user=****
mysql.password=*****

// Hibernate properties

hibernate.show_sql=true
hibernate.hbm2ddl.auto=update


// C3P0 properties

hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150


```



mysql.driver is the param we would use for jdbc driver for MySQL.
mysql.url is the url for our local database. (Usually MySQL will take the port 3306)
mysql.user and mysq.password you will put your user and password of your created database in there.

- In src/main packages, you will have source code that implement the gamelist api.

I will cover about structure of Spring framework in another blog post.

- Features of gamelist api will have: create new game, update game, get game data, and delete the game.

4.Build gamelist api with maven:

Usually to run project from maven, you will run with 'mvn ...'

Since our project use tomcat7, the command line will be:

mvn tomcat7:run

5.Using gamelist api with postman:

Postman is a free tool for users interact with API.
You can download postman from this link: [postman](https://www.getpostman.com/downloads/)

For example, if you want to get all the games from the API, the screenshot looks like this.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/swz1w88uc326677x702h.png)

If you want to create new game:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lyvenzqdjvmycx2kvfc0.png)


Feel free to get the code from my github [gamelist-api](https://github.com/cuongld2/gameListApi)

Next blog post, I will show you about how to test these APIs with Serenity.

Thank you all for reading! :-*

Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :

[<img src="https://thepracticaldev.s3.amazonaws.com/i/cno42wb8aik6o9ek1f89.png">](https://www.buymeacoffee.com/dOaeSPv
)

This will help me to contributing more valued contents.
Enter fullscreen mode Exit fullscreen mode

Top comments (10)

Collapse
 
liyasthomas profile image
Liyas Thomas • Edited

I build postwoman.io API request builder which is a PWA alternative to postman and is open sourced in GitHub.

Story behind Postwoman

Can you write an article on how to make temporary mock APsI for testing purposes with nodejs..? It's a much appreciated feature requests we had.

Collapse
 
cuongld2 profile image
cuongld2

Yo, I will take a look into that.
Thanks for recommending this.
Peace!!

Collapse
 
frandall profile image
Andry Kurniawan

Waiting your next post ^

Collapse
 
cuongld2 profile image
cuongld2

Maybe you might want to take a look:
serenity-automation-part1

Collapse
 
cuongld2 profile image
cuongld2

Will post that soon.
Thanks for your comment :d

Collapse
 
babusr01 profile image
SRIDHAR BABU BANDLAMUDI

can you please post Spring boot restful service article

Collapse
 
cuongld2 profile image
cuongld2

Dear @SRIDHAR BABU BANDLAMUDI,
Maybe you might want to check my new blog post about springboot REST API in here

Collapse
 
cuongld2 profile image
cuongld2

Yeah. Thanks for your comments. I will do that post after I finish the series about Serenity :d.

Collapse
 
caweltjr profile image
John Cawelti

Thanks for taking time and effort to explain in understandable language how to code this very necessary action.

Collapse
 
cuongld2 profile image
cuongld2 • Edited

That's great.
I will try continue doing this.
Thanks a lot!