[英]Integration test for springboot with initalize query
我將在 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);
我的入口點是在 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');
我得到一個錯誤:
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
我不知道我的路徑上有什么問題。我想知道我的測試腳本或策略是否有問題
使用@Sql
注釋時應遵循路徑資源語義。 如果你想從資源文件夾中加載腳本文件,你應該使用classpath:
參考:
@Sql(value = {"classpath:schema.sql"})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.