首页 > 解决方案 > Hibernate JPA 集成测试问题

问题描述

问题

我正在尝试在 Spring 中对我的 REST 控制器进行一些集成测试。我已经设置了一个 BEFORE_TEST sql 脚本来运行以在每次测试之前创建一个表以及一些默认值,但它们似乎没有按预期插入。

这是我的集成测试设置...

 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc

@Sql(scripts = { "classpath:specimen-schema.sql",
        "classpath:specimen-schema.sql" }, executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)

public class SpecimenControllerIntegrationTest {

    @Autowired
    private MockMvc mockMVC;

    @Autowired
    private ObjectMapper mapper;

    @Test
    void testCreate() throws Exception {
        Specimen newSpecimen = new Specimen("Tyrannosaurus_Rex", "California, USA", "Container_A", null, "Jaw Bone");

        String newSpecimenAsJSON = this.mapper.writeValueAsString(newSpecimen);
        RequestBuilder mockRequest = post("/createSpecimen").contentType(MediaType.APPLICATION_JSON)
                .content(newSpecimenAsJSON);

        Specimen savedSpecimen = new Specimen(2L, "Tyrannosaurus_Rex", "California, USA", "Container_A", null,
                "Jaw Bone");
        String savedSpecimenAsJSON = this.mapper.writeValueAsString(savedSpecimen);

        ResultMatcher matchStatus = status().isCreated();
        ResultMatcher matchBody = content().json(savedSpecimenAsJSON);

        this.mockMVC.perform(mockRequest).andExpect(matchStatus).andExpect(matchBody);

    }

这是我的两个 SQL 脚本...

架构...

DROP TABLE IF EXISTS `specimen` CASCADE;
CREATE TABLE specimen (
    id BIGINT AUTO_INCREMENT NOT NULL,
    date_arrived TIMESTAMP,
    description VARCHAR(255),
    latin_name VARCHAR(255) NOT NULL,
    origin VARCHAR(255) NOT NULL,
    storage_location VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

数据...

INSERT INTO `specimen` (`date_arrived`, `description`, `latin_name`, `origin`, `storage_location`) VALUES ('2020-05-05', 'skeleton', 'archaeornithymimus', 'Peru', 'Container-C');

这是我的样本类值和构造函数...

package com.qa.museum.domain;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Specimen {

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

    private String latinName;
    private String origin;
    private String storageLocation;
    private Date dateArrived;
    private String description;

    public Specimen(String latinName, String origin, String storageLocation, Date dateArrived, String description) {
        super();
        this.latinName = latinName;
        this.origin = origin;
        this.storageLocation = storageLocation;
        this.dateArrived = dateArrived;
        this.description = description;
    }

这是我正在测试的控制器的一部分......

@RestController
public class SpecimenController {

    private SpecimenService service;

    public SpecimenController(SpecimenService service) {
        super();
        this.service = service;
    }

    @PostMapping("/createSpecimen")
    public ResponseEntity<Specimen> createSpecimen(@RequestBody Specimen specimen) {
        return new ResponseEntity<Specimen>(this.service.createSpecimen(specimen), HttpStatus.CREATED);
    }

测试结果

测试结果是 id 预期为 2,但它得到 1。它应该是 2,因为 SQL 脚本应该在数据库中创建一个标本实例并将 id 自动增加到 1,然后我的测试应该创建第二个, id 为 2。但是,SQL 脚本似乎没有插入初始 DB 条目。

Hibernate: insert into specimen (id, date_arrived, description, latin_name, origin, storage_location) values (null, ?, ?, ?, ?, ?)

旁注- 我在 Java 中使用的日期不多,所以它可能与日期字段有关吗?当我在 Postman 中测试时它工作正常,我将测试日期条目设置为 null 作为占位符,因为我不确定 java 日期语法。

提前致谢。

标签: javaspringspring-boothibernatespring-data-jpa

解决方案


specimen-schema.sql您两次指定了相同的脚本名称:

@Sql(scripts = { "classpath:specimen-schema.sql",
        "classpath:specimen-schema.sql" }, executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)

插入第一条记录的 sql 脚本从未运行过。

specimen-data.sql

INSERT INTO `specimen` (`date_arrived`, `description`, `latin_name`, `origin`, `storage_location`) VALUES ('2020-05-05', 'skeleton', 'archaeornithymimus', 'Peru', 'Container-C');

所以记录new Specimen("Tyrannosaurus_Rex", "California, USA", "Container_A", null, "Jaw Bone");已经用 id 保存了1

您应该将第二个更改"classpath:specimen-schema.sql""classpath:specimen-data.sql"


推荐阅读