springboot与初始化查询的集成测试

5t7ly7z5  于 2023-01-26  发布在  Spring
关注(0)|答案(1)|浏览(184)

我打算在内存mysql数据库的Spring Boot 中使用集成测试。但是我的测试组件在存储库部分查询如下:

@Query(
             value = "SELECT order_id,title,description,requirement,salary,user_info.name,user_info.contact,date"+
                     " FROM job_order " +
                     " FULL JOIN user_info " +
                     " ON sender_id=user_info.id" +
                     " WHERE order_id= ?1 ;",nativeQuery = true
     )
     Response singlejob(int order_id);

我的入口点在job_order表上做一些事情。查询需要两个表,所以我尝试插入user_info表,然后测试job_order。然后写测试如下:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@TestExecutionListeners(listeners = { SqlScriptsTestExecutionListener.class })

class OrderServiceApplicationTests {
    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private JobRepository jobRepository;


    //@Sql("INSERT INTO user_info(name) VALUES \"alex\" ")
    @Test
    @Sql("{/resources/schema.sql}")
    void shouldCreatePost() throws Exception {
        JobOrder job=jobRepository.save(createjob());
        String request=objectMapper.writeValueAsString(job);

        mockMvc.perform(MockMvcRequestBuilders.post("/Job/Joborder")
                .contentType(MediaType.APPLICATION_JSON)
                .content(request))
                .andExpect(status().is2xxSuccessful());

    }


    JobOrder createjob(){
        JobOrder job=new JobOrder();
        job.setTitle("Hiring software engineer");
        job.setDescription("responsible for developing and maintaining mobile app");
        job.setRequirement("Need to know basic sql springboot,2 years exp");
        job.setSalary(234);
        job.setOrder_id(1);
        return job;

    }
}

sql:插入到用户信息(名称)值(“印度”);
我得到一个错误:

org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [com/example/OrderService/{/resources/schema.sql}]

    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:239)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:54)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.execute(ResourceDatabasePopulator.java:269)
    at org.springframewo

我的财产:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=***
spring.datasource.password=*******
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.defer-datasource-initialization=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.sql.init.mode=always

我的路径:x1c 0d1x
我不知道在我的道路上有什么问题。我想知道是否有问题在我的测试脚本或策略

osh3o9ms

osh3o9ms1#

使用@Sql注解时应遵循路径资源语义。如果要从resources文件夹加载脚本文件,应使用classpath:引用:

@Sql(value = {"classpath:schema.sql"})

相关问题