renz
renz

Reputation: 1070

Intellij classpath error Caused by: java.io.FileNotFoundException

The directories of the Intellij project:

src/main/java/rewards/testdb/schema.sql
src/main/java/rewards/testdb/test-data.sql
src/main/java/rewards/testdb/config/alternate-datasource-config.xml
src/main/java/rewards/testdb/config/AlternateDataSourceConfigTests.java

The bean configuration under test (from alternate-datasource-config.xml):

<bean id="dataSource-createdFromList" class="org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean" lazy-init="true">
    <property name="databaseName" value="rewards" />
    <property name="databasePopulator">
        <bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
            <property name="scripts">
                <util:list>
                    <value>classpath:rewards/testdb/schema.sql</value>
                    <value>classpath:rewards/testdb/test-data.sql</value>
                </util:list>
            </property>
        </bean>
    </property>
</bean>

I'm encountering an error in my unit test:

Caused by: java.io.FileNotFoundException: class path resource [rewards/testdb/schema.sql] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)

However, if I modified the directories to:

src/main/java/rewards/testdb/config/schema.sql
src/main/java/rewards/testdb/config/test-data.sql
src/main/java/rewards/testdb/config/alternate-datasource-config.xml
src/main/java/rewards/testdb/config/AlternateDataSourceConfigTests.java

and modify my bean config:

<bean id="dataSource-createdFromList" class="org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean" lazy-init="true">
        <property name="databaseName" value="rewards" />
        <property name="databasePopulator">
            <bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
                <property name="scripts">
                    <util:list>
                        <value>classpath:rewards/testdb/config/schema.sql</value>
                        <value>classpath:rewards/testdb/config/test-data.sql</value>
                    </util:list>
                </property>
            </bean>
        </property>
    </bean>

...my test passes.

Why?

Upvotes: 0

Views: 6996

Answers (2)

Ramazan Yildiz
Ramazan Yildiz

Reputation: 71

In your idea project configuration, point target path as your projects target folder: Edit Configurations->Deployment->Edit Artifact(Click on pencil icon)-> set "Output Directory" input as "your_project_folder_path/target " Then whenever you edit jdbc.properties file, this will take effect with a restart.

This is valid for maven web projects.

Upvotes: 1

marthursson
marthursson

Reputation: 3300

Spring will interpret the resource references as relative (to your test class). You should always make sure to use absolute paths in your resource references, i.e.:

<value>classpath:/rewards/testdb/schema.sql</value>
<value>classpath:/rewards/testdb/test-data.sql</value>

(note the initial forward slash in the resource specifications)

Upvotes: 0

Related Questions