简体   繁体   English

带有初始化查询的 springboot 集成测试

[英]Integration test for springboot with initalize query

I am going use an integration test in spring boot with in memory mysql database.But my test component in repository part query like:我将在 spring 引导中使用集成测试,在 memory mysql 数据库中。但是我在存储库部分查询中的测试组件如下:

@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);

My entrypoint is doing stuff on the job_order table.The query require two tables,so i am trying to insert the user_info table and then test for the job_order.Then a write test like this:我的入口点是在 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;

    }
}

schema.sql: INSERT INTO user_info (name) VALUES ('India'); schema.sql: INSERT INTO user_info (name) VALUES ('India');

and i got an error:我得到一个错误:

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

My properties:我的属性:

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

My Path:我自己的路: 在此处输入图像描述

I dont know what is the problem on my path.And i am wondering if there are problem on my test script or strategy我不知道我的路径上有什么问题。我想知道我的测试脚本或策略是否有问题

Path resource semantics should be followed when @Sql annotation is used.使用@Sql注释时应遵循路径资源语义。 If you want to load the script file from the resources folder you should use classpath: reference:如果你想从资源文件夹中加载脚本文件,你应该使用classpath:参考:

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

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

相关问题 Springboot 集成测试 - 不需要的 Mocks - Springboot Integration Test - Unwanted Mocks SpringBoot测试中的@ActiveProfile(“ integration-test”)无法解析 - @ActiveProfile(“integration-test”) in SpringBoot test not resolving 无法在 SpringBoot 的集成测试中执行 @Autowired bean - Cannot perform @Autowired a bean on integration Test for SpringBoot 如何在 Springboot 集成测试中获取 JPA 上下文? - How to get JPA context in Springboot integration test? 在 SpringBoot 中填充 MongoDb TestContainers 以进行集成测试 - Populate MongoDb TestContainers in a SpringBoot for integration test 在 SpringBoot 集成测试中使用 TestContainers 填充数据库 - Populate a database with TestContainers in a SpringBoot integration test kotlin Springboot mockito集成测试-EntityManger - kotlin Springboot mockito integration test - EntityManger 使用 jsonPath 时 springboot 集成测试失败 - springboot Integration test fail when using jsonPath Springboot @DataJpaTest 与外部 sql server db 的集成测试 - Springboot @DataJpaTest Integration test with external sql server db SpringBoot - 在集成测试中通过“new”关键字创建模拟有状态 object - SpringBoot - Mock stateful object created via “new” keyword in integration test
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM