0

I lean Spring Boot with Book.(copy and paste from book to editor)

There is I can't understand and not working code.

Could you teach me How can I fix and what should I learn?

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

Please check above code, @Sql("/testdata.sql") is not work.

Junit result is like that.

Caused by: java.io.FileNotFoundException: class path resource [testdata.sql] cannot be opened because it does not exist

So I wrote code that is comment out now. It worked.

I think that have to rewrite @Sql argument. But I don't know how to do that.

Could you teach me how to rewrite code?

Is this problem come from lack of my Maven directory rule?

Appendix

Directory tree is below.

tree /F C:\Users\<username>\Desktop\springsample\src\test
C:\USERS\<username>\DESKTOP\SPRINGSAMPLE\SRC\TEST
├─java
│  └─com
│      └─example
│          └─springsample
│                  DemoApplicationTests.java
│                  UserDaoTest.java //Test code in this file.
│
└─resources
        testdata.sql

Thank you for reading.

Additional Info

I read reference,then tried below setting is work. In this case, need to put the .sql file same directory as Test class file. I think can assignment specified location. But I can't.

@Sql("testdata.sql")

■Directory Tree

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

1 Answer 1

2

@Sql by default looks for the files in the package of the @Test class.

for you case it should be as below

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for reply. I made directory , and move .sql file there that you taught me. But FileNotFoundException is still occured by same reason.
remove the '/' in the front - its just @Sql("testdata.sql"). see docs.spring.io/spring-framework/docs/current/javadoc-api/org/…
Thank you for reply and ginving information. I removed '/' , and ran test but same result.... I searched other article and information. According to that same you tought me. So,I don't know why my code dosen't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.