Reputation: 11
I know there's a lot of questions and answers about this, but I've tried everything I've read and you can be sure it has been a lot. I'm using Spring Tool Suite 3.1.0 (but without Spring yet), embedded Maven 3.0.4 and JUnit 4. Ubuntu 12.04. I have this JUnit 4 test:
@Test
public final void testCheckAudio() {
final InputStream stream = AudioServiceImplTest.class.getResourceAsStream("/test.wav");
assertNotNull(stream);
}
test.wav is in src/test/resources and I've checked it's copied to target/test-classes. When I do Run as ... Maven Test I got: ... Failed tests: testCheckAudio(edu.gerher.askitcollector.test.services.AudioServiceImplTest) ... Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project ...
Tests run: 16, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec <<< FAILURE! testCheckAudio(MyPackage.AudioServiceImplTest) Time elapsed: 0.005 sec <<< FAILURE! java.lang.AssertionError at edu.gerher.askitcollector.test.services.AudioServiceImplTest.testCheckAudio(AudioServiceImplTest.java:238) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
I've tried with "test.wav", "/test.wav", creating the test package in src/test/resources and copying also the file there and trying "/MyPackage/test.wav", "MyPackage/test.wav". Always the same answer. It can't find the file.
I've tried Run as... JUnit Test and the same asnwers. En el pom.xml I have
<build>
...
<resources>
<resource>
<directory>.../src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>.../src/test/resources</directory>
</testResource>
</testResources>
<directory>.../target</directory>
Really, thanks for any help you can give me.
Upvotes: 1
Views: 3693
Reputation: 127
This worked for me. I need to read image files from src/test/resource/WsqImages
File filepath= new File("src" + File.separator + "test" + File.separator
+ "resources" + File.separator + "WSQImages");
try (InputStream is = new BufferedInputStream(new FileInputStream(
filepath))) {
Bitmap bitmap = WSQJavaUtil.getBitMap(IOUtils.toByteArray(is));
assertNotNull("BitMap Creation faild", bitmap);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
throw new WSQDecompressionException(e.getMessage(), e);
}
Upvotes: 0
Reputation: 2332
I don't know if it is still relevant but:
Assume you have an image under src/test/resources/tree.png. With the standard maven settings you can reference it in the code like this:
img = ImageIO.read(new File("src/test/resources/tree.png"));
This is relative to the current project location.
If you package your project in a jar file and want to use the resources in the running code, you should also look at Java ResourceLoader. Here is a starting point: java - how do you make a resourceloader?
Upvotes: 1
Reputation: 76
Why have you changed the Maven defaults for resource locations? What should "..." point to?
My suggestion: 1. remove the custom resources configuration 2. put the desired file in src/test/resources 3. reference it with getResourceAsStream("/test.wav");
Upvotes: 0