[英]Hibernate JPA Integration Test issue
问题
我正在尝试对 Spring 中的 REST controller 进行一些集成测试。 我已经设置了一个 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');
这是我的样本 class 值和构造函数...
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;
}
这是我正在测试的 controller 的一部分……
@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 脚本似乎没有插入初始数据库条目。
Hibernate: insert into specimen (id, date_arrived, description, latin_name, origin, storage_location) values (null, ?, ?, ?, ?, ?)
旁注- 我在 Java 中使用的日期不多,所以这可能与日期字段有关吗? 当我在 Postman 中测试时它工作正常,我将测试日期条目设置为 null 作为占位符,因为我不确定 java 日期语法。
提前致谢。
您指定了相同的脚本名称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"
:specimen-schema.sql”更改为"classpath:specimen-data.sql"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.