I didn't want to load the entire Spring Boot configuration for unit-testing my DAO
layer, and therefore created a nested configuration class to suppress default configurations. But when I try to specify SQL scripts for it to run before tests, its unable to find them.
Here's the code:
package com.test.customer.controller;
..
@RunWith(SpringRunner.class)
@JdbcTest
@Sql({"data.sql"})
public class InterviewInformationControllerTest {
@Configuration
static class TestConfiguration{
}
@Test
public void testCustomer() {
// code
}
}
I get the error: Cannot read SQL script from class path resource [com/test/customer/controller/data.sql]; nested exception is java.io.FileNotFoundException: class path resource [com/test/customer/controller/data.sql] cannot be opened because it does not exist
I've tried placing the file at both src/main/resources
(not preferred) as well as at src/test/resources
(which I prefer)
Note: I'm running the Unit test from inside Eclipse by doing Run as -> JUnit test
.
Edit: Added the static
keyword to the configuration class
your inner configuration class will not work unless you add a static keyword before its definition. However you should know that for the @Sql annotation
Path Resource Semantics
Each path will be interpreted as a Spring Resource. A plain path — for example, "schema.sql" — will be treated as a classpath resource that is relative to the package in which the test class is defined. A path starting with a slash will be treated as an absolute classpath resource, for example: "/org/example/schema.sql". A path which references a URL (e.g., a path prefixed with classpath:, file:, http:, etc.) will be loaded using the specified resource protocol.
So try to prefix the value inside @Sql
with classpath:
like this :
@Sql(scripts={"classpath:data.sql"})
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With