Udo Held
Udo Held

Reputation: 12548

FileNotFoundException in test case run with maven using Spring resource loader

Our program is sending mails. For sending mails it includes an attachment.

The project is set up with maven. The JUnit test case loads the spring configuration first. Spring is loading the File with its DefaultResourceLoader. When running this directly within the workspace its running fine, however as a maven test it fails.

Spring Configuration for resource loader:

<property name="defaultResourceLoader">
    <bean class="org.springframework.core.io.DefaultResourceLoader" />
</property>

ImplementingClass:

@Service
public class SomeClassA {

     @Autowired
     private DefaultResourceLoader defaultResourceLoader; 

     @Value("${mailLogoPath}")
     private String mailLogo; 

     public void someMethod(){
         String filePath = defaultResourceLoader.getResource(mailLogo).getFile().getAbsolutePath();   
     }
}

The exception points to the actual problem and difference:

java.io.FileNotFoundException: class path resource [templates/efmaillogo.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/IBM/workspace/efMy10/ef-mvxsrv-reactor/ef-mvxsrv-service-resources/target/ef-mvxsrv-service-resources-1.5.23-SNAPSHOT.jar!/templates/efmaillogo.jpg at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:204) at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52) at SomeClassA.someMethod(SomeClassA.java:9)

Spring tries to look the file up within the jar. When running it as a junit test within eclipse, everything is working, because spring finds the file directly within the workspace. However when running the same unit test with maven it fails, because it doesn't find the file, because its in a jar in a different module. The file in the other module is needed by different modules, so I don't want to move it. On the application server where everything is deployed its working as well, because the .ear file is expanded.

I'm wondering if there is a different way to access the file with Spring, so that I don't have to skip this test case with maven.

Update Tried doing a lookup for the file with:

 String filePath = defaultResourceLoader.getResource(mailLogo).getURL.getFile();

However it now fails when actually sending the mail with Transport.send(msg).

java.io.FileNotFoundException: file:\C:\Users\uhe.m2\repository\com\clavisit\ef\mvxsrv\ef-mvxsrv-service-resources\1.5.23-SNAPSHOT\ef-mvxsrv-service-resources-1.5.23-SNAPSHOT.jar!\templates\efmaillogo.jpg (The filename, directory name, or volume label syntax is incorrect.) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at com.clavisit.ef.ep.service.integration.handler.mail.SendMail.sendMessage(SendMail.java:153)

Upvotes: 2

Views: 5420

Answers (1)

Maniganda Prakash
Maniganda Prakash

Reputation: 4732

String filePath = defaultResourceLoader.getResource(mailLogo).getURL().getFile();

The above code change must work as per the discussion in this thread http://www.coderanch.com/t/474047/Spring/Spring-cannot-find-file-classpath

Upvotes: 2

Related Questions