Spring Data JPA & Hibernate with Cucumber

Link to the full version of the project on GitHub:
Github SpringDataJPA

Every QA Automation engineer and developer very often faces the task of getting the records from the database. Let’s write a small implementation of work with H2 relational database, using Spring Data JPA & Hibernate libraries. Let’s see how much Spring helps to reduce code when working with queries and run some scripts with Cucumber framework to understand how this approach can be applied to writing autotests.

Used:

Spring Data JPA Hibernate H2 Database Cucumber Junit 5

It is necessary to perform:
– Implement a controller for database queries (Spring Data JPA);
– Write several tests in Cucumber;
– Launch the project from the console;

Create a maven project and connect the necessary dependencies in pom.xml

Connecting the Spring framework’s library

            org.springframework
            spring-context
            ${spring.version}
 
 
            org.springframework
            spring-orm
            ${spring.version}
 
 
            org.springframework.data
            spring-data-jpa
            2.1.5.RELEASE
 
 
            org.springframework
            spring-test
            ${spring.version}

The Hibernate library as an implementation of the JPA will be used

            org.hibernate
            hibernate-core
            ${hibernate.version}

The driver to connect to the database

            com.h2database
            h2
            ${h2.version}

Let’s add Lombok for cleaner code, with implementation of, for example, heteros – setters via annotations.

            org.projectlombok
            lombok
            ${lombok.version}

We will use the Cucumber and Junit 5 frameworks to run the tests

            io.cucumber
            cucumber-java
            ${cucumber.version}
 
 
            io.cucumber
            cucumber-junit
            ${cucumber.version}
 
 
            io.cucumber
            cucumber-spring
            ${cucumber.version}
 
 
 
            org.junit.jupiter
            junit-jupiter-api
            ${junit.jupiter.version}
 
 
            org.junit.jupiter
            junit-jupiter-engine
            ${junit.jupiter.version}

The creation of tables and filling them with test data will be fully responsible for Spring. To do this, let’s set up the configuration file application.properies

spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
em.packages.scan=ru.gotoqa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.initialization-mode=always
spring.datasource.platform=h2

The project is set up, we can move on to writing code.

@Entity annotation is used to map the PersonEntity class to the Person table. The id field is labeled @Id and @GeneratedValue, it is a primary key and its value is automatically generated.

For the Person table

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Person", schema = "actors")
public class PersonEntity {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
 
    @Column(name = "first_name")
    private String firstName;
 
    @Column(name = "last_name")
    private String lastName;
    private Date birthday;
    private String email;
    private String phone;
    private String job;
 
}

For the Geography table, the Entity description will be augmented with a static primary key (PK) class – this value will be the unique Id key that is required for Hibernate to work correctly.

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Geography")
public class GeographyEntity {
 
    @EmbeddedId
    private PK pk;
 
    @Basic
    private Integer population;
 
    @Basic
    private String language;
 
    @Embeddable
    @Data
    public static class PK implements Serializable {
 
        @Basic
        @Column(name = "country", insertable = false, updatable = false)
        private String country;
 
        @Basic
        @Column(name = "city", insertable = false, updatable = false)
        private String city;
    }
}

Project structure:

ru.gotoqa.config – The directory contains the Spring configuration classes, database connection parameters, etc.
ru.gotoqa.entity – Directory for mapping entities to tables
ru.gotoqa.repository – Contains interfaces that extend the CrudRepository interface from the Spring Data JPA
ru.gotoqa.service – Service classes for transaction management, separates the business layer from the DAO implementation in the repository
ru.gotoqa.steps – Defines the implementation of the Cucumber framework steps
src/test/java/resources/*.feature – Directory for writing test scripts (feature) of the Cucumber framework

As in the textbook, the tests do not see the services and implementation of the database, each layer is solely responsible for its own work.

After running the tests, we get a report in the console.

Feature: Сheck records in a db table
 
  Scenario: Check records in Person table (should be 5)
    Then get all: 5 records from Person table
 
  Scenario: Check records in Geography table (should be 4)
    Then get all: 4 records from Geography table
2 Scenarios (2 passed)
2 Steps (2 passed)
0m0,243s

Link to the full version of the project on GitHub:
Github SpringDataJPA

Releated Post