Queries using Spring jpa + Cucumber + JUnit in automated test project

1.6k Views Asked by At

I have been trying to have an automated test in which I can query the database for the status of an order that I create by UI (Appium) on a device (Android or IOS).

I wish I could do the crud with the database using spring jpa since I have been told that it is a better practice.

I have tried to implement it but when declaring the repository object, it always reaches null so it never uses the repository method that I have created. I have the following sequence.

Runner (JUnit) -> Feature (Gherkin) ---> Step Definitios ---> Steps.

OrderWithouUsingDelayOptionCashiOSTest.java (Runner)

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(Cucumber.class)
@SpringBootTest
@CucumberOptions(
    plugin = {"pretty", "com.rappi.setup.reports.TestListener"},
    features = {"classpath:com/rappi/features/orders/order_restaurant_ios.feature"},
    glue = {"com.rappi.glue.hooks", "com.rappi.definitions"},
    tags = "@iOSTest and @Cash")
public class OrderWithoutUsingDelayOptionCashiOSTest {}

order_restaurant_ios.feature

#language: es
@iOSTest @After-restaurant-order @AfterLogoutMS
Característica: Crear orden SIN usar la opción del delay ¿Quieres Modificar algo? con diferentes metodos de pago - iOS

  Antecedentes: Autenticacion del usuario y creacion de carrito por ms
    Dado que el proceso de autenticacion del usuario se completo en el pais de: Colombia
    Y que se tiene el producto 810017633 de la tienda 900102662 agregado en canasta
.
.
.
Other things

LoginRappiDefinition.java (Step definitions)

  @Cuando("^que el proceso de autenticacion del usuario se completo en el pais de: (.*)$")
  public void completeUserLogin(String country) {
    loginRappiStep.selectLocationCountry(country);
  }

LoginRappiStep.java (Step).

This class is where the orderRepository object always reaches null so it never calls the "getOne (order)" method.

public class LoginRappiStep {

  @Autowired public OrderRepository orderRepository;

  public void selectLocationCountry() {
    long order = 5019023;
    Order orderEntity = orderRepository.getOne(order);
  }

}

Order.java (Entity)


import javax.persistence.*;

@Entity
@Table(name = "orders")
public class Order {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column private String state;

  public Order(Long id, String state) {
    this.id = id;
    this.state = state;
  }

  public Long getId() {
    return id;
  }

  public Order setId(Long id) {
    this.id = id;
    return this;
  }

  public String getState() {
    return state;
  }

  public Order setState(String state) {
    this.state = state;
    return this;
  }
}

OrderRepository.java

import com.rappi.entities.Order;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderRepository extends JpaRepository<Order, Long> {

  Optional<Order> findById(Long id);

  Order getOne(Long id);
}

build.gradle

dependencies {
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.3.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.3.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.3.4.RELEASE'
}

application.properties

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://postgres-core-orders.dev.com:5432/grability
spring.datasource.username=sa
spring.datasource.password=sa

Anyone know what else should I do, what am I overlooking? This is not a spring project, it is a normal java project.

I appreciate any help you can give me.

1

There are 1 best solutions below

1
M.P. Korstanje On

You've annotated the wrong class with @SpringBootTest. Try:

package com.rappi.config

import org.springframework.boot.test.context.SpringBootTest;

import io.cucumber.spring.CucumberContextConfiguration;

@CucumberContextConfiguration
@SpringBootTest
public class CucumberSpringConfiguration {

}

And make sure that the CucumberSpringConfiguration is on the glue path by adding it to:

glue = {"com.rappi.config", "com.rappi.glue.hooks", "com.rappi.definitions"},

Cucumber uses Springs TestContextManager. The class annotated with @CucumberContextConfiguration will be used to create the test application context. So any other annotations you put on CucumberSpringConfiguration will also be picked up by Spring.

See: https://github.com/cucumber/cucumber-jvm/blob/main/spring/