Skip to content

Commit 4d6cb52

Browse files
Yatendra GoelYatendra Goel
Yatendra Goel
authored and
Yatendra Goel
committed
BAEL-1978: Added a test to demostrate classpath resource with relative path
1 parent 3b30ae7 commit 4d6cb52

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

spring-core/src/test/java/com/baeldung/resource/SpringResourceIntegrationTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.IOException;
88
import java.io.InputStream;
99
import java.io.InputStreamReader;
10+
import java.net.URL;
11+
import java.net.URLClassLoader;
1012
import java.nio.file.Files;
1113
import java.util.stream.Collectors;
1214

@@ -23,6 +25,8 @@
2325
import org.springframework.test.context.support.AnnotationConfigContextLoader;
2426
import org.springframework.util.ResourceUtils;
2527

28+
import com.baeldung.dependson.config.TestConfig;
29+
2630
/**
2731
* Test class illustrating various methods of accessing a file from the classpath using Resource.
2832
* @author tritty
@@ -98,4 +102,19 @@ public void whenResourceAsFile_thenReadSuccessful() throws IOException {
98102
final String employees = new String(Files.readAllBytes(resource.toPath()));
99103
assertEquals(EMPLOYEES_EXPECTED, employees);
100104
}
105+
106+
@Test
107+
public void whenClassPathResourceWithAbsoultePath_thenReadSuccessful() throws IOException {
108+
final File resource = new ClassPathResource("/data/employees.dat", this.getClass()).getFile();
109+
final String employees = new String(Files.readAllBytes(resource.toPath()));
110+
assertEquals(EMPLOYEES_EXPECTED, employees);
111+
}
112+
113+
@Test
114+
public void whenClassPathResourceWithRelativePath_thenReadSuccessful() throws IOException {
115+
// final File resource = new ClassPathResource("../../../data/employees.dat", SpringResourceIntegrationTest.class).getFile();
116+
final File resource = new ClassPathResource("/data/employees.dat", SpringResourceIntegrationTest.class).getFile();
117+
final String employees = new String(Files.readAllBytes(resource.toPath()));
118+
assertEquals(EMPLOYEES_EXPECTED, employees);
119+
}
101120
}

0 commit comments

Comments
 (0)