spring boot 正在执行 data.sql 和 schema.sql 两次?
spring boot is executing data.sql and schema.sql twice?
我正在尝试测试 Spring 启动应用程序。我正在使用内存中的 H2 DB,我想用一些数据初始化它,并在项目 table 中添加一个元素后通过比较数据库中的数据数量来实施集成测试,但测试是失败,当我尝试跟踪执行时,data.sql 和 schema.sql 被执行了两次。
测试class
@ContextConfiguration(classes=ProjectManagementApplication.class)
@RunWith(SpringRunner.class)
@DataJpaTest
@SqlGroup(@Sql( executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts= {"classpath:schema.sql","classpath:data.sql"}))
public class ProjectRepositoryIntegrationTest {
@Autowired
ProjectRepository proRepo;
@Autowired
EmployeeRepository empRepo;
@Test
public void ifNewProjectSaved_thenSuccess() {
Project newProject=new Project("new Project","COMPLETED","test Description");
proRepo.save(newProject);
List<Project> pro= proRepo.findAll();
for(int i=0;i<pro.size();i++) {
System.out.println(pro.get(i));
}
assertEquals(5,pro.size());
}
}
架构文件
CREATE SEQUENCE IF NOT EXISTS employee_seq;
CREATE TABLE IF NOT EXISTS employee (
employee_id BIGINT NOT NULL DEFAULT nextval('employee_seq') PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
lastname VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);
CREATE SEQUENCE IF NOT EXISTS project_seq;
CREATE TABLE IF NOT EXISTS project (
project_id BIGINT NOT NULL DEFAULT nextval('project_seq') PRIMARY KEY,
name VARCHAR(100) NOT NULL,
stage VARCHAR(100) NOT NULL,
description VARCHAR(500) NOT NULL
);
CREATE TABLE IF NOT EXISTS employee_project (
project_id BIGINT REFERENCES project,
employee_id BIGINT REFERENCES employee
);
data.sql
-- INSERT EMPLOYEES
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'John', 'Warton', 'warton@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Mike', 'Lanister', 'lanister@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Steve', 'Reeves', 'Reeves@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Ronald', 'Connor', 'connor@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Jim', 'Salvator', 'Sal@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Peter', 'Henley', 'henley@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Richard', 'Carson', 'carson@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Honor', 'Miles', 'miles@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Tony', 'Roggers', 'roggers@gmail.com');
-- INSERT PROJECTS
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'Large Production Deploy', 'NOTSTARTED', 'This requires all hands on deck for the final deployment of the software into production');
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'New Employee Budget', 'COMPLETED', 'Decide on a new employee bonus budget for the year and figureout who will be promoted');
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'Office Reconstruction', 'INPROGRESS', 'The office building in Monroe has been damaged due to hurricane in the region. This needs to be reconstructed');
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'Improve Intranet Security', 'INPROGRESS', 'With the recent data hack, the office security needs to be improved and proper security team needs to be hired for implementation');
-- INSERT PROJECT_EMPLOYEE_RELATION
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'Large Production Deploy');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'New Employee Budget');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'Office Reconstruction');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Reeves' AND p.name = 'Large Production Deploy');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'New Employee Budget');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'Improve Intranet Security');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Henley' AND p.name = 'Office Reconstruction');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Henley' AND p.name = 'Improve Intranet Security');
Spring Boot 的DataSource
初始化将自动应用 classpath:schema.sql
和 classpath:data.sql
文件,而无需在您的测试中使用 @SqlGroup
和 @Sql
class.
您应该从测试 class 中删除 @SqlGroup
和 @Sql
或禁用 [=10=] 初始化。要实现后者,请设置 spring.datasource.initialization-mode=never
(Spring Boot 2.4 及更早版本)或 spring.sql.init.mode=never
(Spring Boot 2.5 及更高版本)。
我正在尝试测试 Spring 启动应用程序。我正在使用内存中的 H2 DB,我想用一些数据初始化它,并在项目 table 中添加一个元素后通过比较数据库中的数据数量来实施集成测试,但测试是失败,当我尝试跟踪执行时,data.sql 和 schema.sql 被执行了两次。
测试class
@ContextConfiguration(classes=ProjectManagementApplication.class)
@RunWith(SpringRunner.class)
@DataJpaTest
@SqlGroup(@Sql( executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts= {"classpath:schema.sql","classpath:data.sql"}))
public class ProjectRepositoryIntegrationTest {
@Autowired
ProjectRepository proRepo;
@Autowired
EmployeeRepository empRepo;
@Test
public void ifNewProjectSaved_thenSuccess() {
Project newProject=new Project("new Project","COMPLETED","test Description");
proRepo.save(newProject);
List<Project> pro= proRepo.findAll();
for(int i=0;i<pro.size();i++) {
System.out.println(pro.get(i));
}
assertEquals(5,pro.size());
}
}
架构文件
CREATE SEQUENCE IF NOT EXISTS employee_seq;
CREATE TABLE IF NOT EXISTS employee (
employee_id BIGINT NOT NULL DEFAULT nextval('employee_seq') PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
lastname VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);
CREATE SEQUENCE IF NOT EXISTS project_seq;
CREATE TABLE IF NOT EXISTS project (
project_id BIGINT NOT NULL DEFAULT nextval('project_seq') PRIMARY KEY,
name VARCHAR(100) NOT NULL,
stage VARCHAR(100) NOT NULL,
description VARCHAR(500) NOT NULL
);
CREATE TABLE IF NOT EXISTS employee_project (
project_id BIGINT REFERENCES project,
employee_id BIGINT REFERENCES employee
);
data.sql
-- INSERT EMPLOYEES
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'John', 'Warton', 'warton@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Mike', 'Lanister', 'lanister@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Steve', 'Reeves', 'Reeves@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Ronald', 'Connor', 'connor@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Jim', 'Salvator', 'Sal@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Peter', 'Henley', 'henley@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Richard', 'Carson', 'carson@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Honor', 'Miles', 'miles@gmail.com');
insert into employee (employee_id, first_name, lastname, email) values (nextval('employee_seq'), 'Tony', 'Roggers', 'roggers@gmail.com');
-- INSERT PROJECTS
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'Large Production Deploy', 'NOTSTARTED', 'This requires all hands on deck for the final deployment of the software into production');
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'New Employee Budget', 'COMPLETED', 'Decide on a new employee bonus budget for the year and figureout who will be promoted');
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'Office Reconstruction', 'INPROGRESS', 'The office building in Monroe has been damaged due to hurricane in the region. This needs to be reconstructed');
insert into project (project_id, name, stage, description) values (nextval('project_seq'), 'Improve Intranet Security', 'INPROGRESS', 'With the recent data hack, the office security needs to be improved and proper security team needs to be hired for implementation');
-- INSERT PROJECT_EMPLOYEE_RELATION
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'Large Production Deploy');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'New Employee Budget');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'Office Reconstruction');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Reeves' AND p.name = 'Large Production Deploy');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'New Employee Budget');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Warton' AND p.name = 'Improve Intranet Security');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Henley' AND p.name = 'Office Reconstruction');
insert into employee_project (employee_id, project_id) (select e.employee_id, p.project_id from employee e, project p where e.lastname ='Henley' AND p.name = 'Improve Intranet Security');
Spring Boot 的DataSource
初始化将自动应用 classpath:schema.sql
和 classpath:data.sql
文件,而无需在您的测试中使用 @SqlGroup
和 @Sql
class.
您应该从测试 class 中删除 @SqlGroup
和 @Sql
或禁用 [=10=] 初始化。要实现后者,请设置 spring.datasource.initialization-mode=never
(Spring Boot 2.4 及更早版本)或 spring.sql.init.mode=never
(Spring Boot 2.5 及更高版本)。