[英]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
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.