[英]Integration test in spring boot cant found the path of sql file
I am writing a integration test for rest api entry.The api require to initialize database before the test.However it gives an error which showing it cant find any path to my sql file.我正在为 rest api 条目编写集成测试。api 需要在测试前初始化数据库。但是它给出了一个错误,显示它无法找到我的 sql 文件的任何路径。
@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我不知道我的路径上有什么问题。我想知道我的测试脚本或策略是否有问题
Several things are wrong with your @Sql("{/resources/schema.sql}")
.您的
@Sql("{/resources/schema.sql}")
有几处错误。
the {
and }
do not belong into the path, why did you add it in the first place? {
和}
不属于路径,为什么要首先添加它?
You don't need to include the /resources
directory in the path.您不需要在路径中包含
/resources
目录。 To find out what the path of your file is, open the target/test-classes
and have a look.要找出文件的路径是什么,请打开
target/test-classes
并查看。 Everything that resides in the resources
directory will be placed in the root directory. resources
目录中的所有内容都将放在根目录中。 If your file was in resources/sql/test.sql
it would be available under sql/test.sql
.如果您的文件位于
resources/sql/test.sql
中,它将在sql/test.sql
下可用。
@Sql("schema.sql")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.