简体   繁体   English

spring boot 正在执行 data.sql 和 schema.sql 两次?

[英]spring boot is executing data.sql and schema.sql twice?

I was trying to test a Spring Boot Application.我试图测试一个 Spring Boot 应用程序。 I am using an in-memory H2 DB that I would like to initialize with some data and implement an integration test by comparing the number of data in the database after I have added one element in the project table but the test is failing and when I try to track the execution the data.sql and schema.sql are executed two times.我正在使用内存中的 H2 DB,我想用一些数据初始化它,并在我在项目表中添加一个元素但测试失败后,通过比较数据库中的数据数量来实现集成测试尝试跟踪 data.sql 和 schema.sql 执行两次的执行情况。

test 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());
    }

}

schema file模式文件

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数据.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's DataSource initialization will apply classpath:schema.sql and classpath:data.sql files automatically without using @SqlGroup and @Sql on your test class. Spring Boot 的DataSource初始化将自动应用classpath:schema.sql classpath:data.sqlclasspath:data.sql文件,而无需在测试类上使用@SqlGroup@Sql

You should either remove @SqlGroup and @Sql from your test class or disable DataSource initialization.您应该从测试类中删除@SqlGroup@Sql或禁用DataSource初始化。 To achieve the latter set spring.datasource.initialization-mode=never (Spring Boot 2.4 and earlier) or spring.sql.init.mode=never (Spring Boot 2.5 and later).要实现后者,请设置spring.datasource.initialization-mode=never (Spring Boot 2.4 及更早版本)或spring.sql.init.mode=never (Spring Boot 2.5 及更高版本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM