Spring boot 当我使用@Sql进行Spring测试时,发生FileNotFoundException
我把书靠在弹簧靴上。(从书上复制粘贴到编辑器上) 有一个我不能理解并且不能工作的代码 你能教我怎么修理吗?我应该学什么Spring boot 当我使用@Sql进行Spring测试时,发生FileNotFoundException,spring-boot,unit-testing,gradle,junit,spring-test,Spring Boot,Unit Testing,Gradle,Junit,Spring Test,我把书靠在弹簧靴上。(从书上复制粘贴到编辑器上) 有一个我不能理解并且不能工作的代码 你能教我怎么修理吗?我应该学什么 @Test //@Sql("file:C:/Users/<username>/Desktop/springsample/src/test/resources/testdata.sql") //This is work! @Sql("/testdata.sql") // This is not work. public void
@Test
//@Sql("file:C:/Users/<username>/Desktop/springsample/src/test/resources/testdata.sql") //This is work!
@Sql("/testdata.sql") // This is not work.
public void countTest2(){
assertEquals(dao.count(), 3);
}
谢谢你的阅读
其他信息
我读了参考资料,然后试着在下面设置是工作。
在这种情况下,需要将.sql文件放在与测试类文件相同的目录中。
我想你可以指定地点。但我不能
@Sql("testdata.sql")
■目录树
C:\USERS\<username>\DESKTOP\SPRINGSAMPLE\SRC\TEST
├─java
│ └─com
│ └─example
│ └─springsample
│ DemoApplicationTests.java
│ UserDaoTest.java //Test code in this file.
│ testdata.sql
└─resources
└─com
└─example
└─springsample
C:\USERS\\DESKTOP\SPRINGSAMPLE\SRC\TEST
├─JAVA
│ └─通用域名格式
│ └─例子
│ └─春样
│ DemoApplicationTests.java
│ UserDaoTest.java//测试此文件中的代码。
│ testdata.sql
└─资源
└─通用域名格式
└─例子
└─春样
@Sql默认情况下会在@Test类的包中查找文件
对于您的情况,应如下所示
C:\USERS\<username>\DESKTOP\SPRINGSAMPLE\SRC\TEST
├─java
│ └─com
│ └─example
│ └─springsample
│ DemoApplicationTests.java
│ UserDaoTest.java //Test code in this file.
│
└─resources
│ └─com
│ └─example
│ └─springsample
│ testdata.sql
C:\USERS\\DESKTOP\SPRINGSAMPLE\SRC\TEST
├─JAVA
│ └─通用域名格式
│ └─例子
│ └─春样
│ DemoApplicationTests.java
│ UserDaoTest.java//测试此文件中的代码。
│
└─资源
│ └─通用域名格式
│ └─例子
│ └─春样
│ testdata.sql
谢谢您的回复。我创建了目录,并将.sql文件移动到您教我的位置。但是FileNotFoundException仍然由于相同的原因发生。请删除前面的“/”-它只是@Sql(“testdata.Sql”)。请参阅“谢谢您的回复和信息”。我删除了“/”,并运行了测试,但结果相同。。。。我搜索了其他文章和信息。根据你对我的看法。所以,我不知道为什么我的代码不起作用。
C:\USERS\<username>\DESKTOP\SPRINGSAMPLE\SRC\TEST
├─java
│ └─com
│ └─example
│ └─springsample
│ DemoApplicationTests.java
│ UserDaoTest.java //Test code in this file.
│
└─resources
│ └─com
│ └─example
│ └─springsample
│ testdata.sql