|
7 | 7 | import java.io.IOException;
|
8 | 8 | import java.io.InputStream;
|
9 | 9 | import java.io.InputStreamReader;
|
| 10 | +import java.net.URL; |
| 11 | +import java.net.URLClassLoader; |
10 | 12 | import java.nio.file.Files;
|
11 | 13 | import java.util.stream.Collectors;
|
12 | 14 |
|
|
23 | 25 | import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
24 | 26 | import org.springframework.util.ResourceUtils;
|
25 | 27 |
|
| 28 | +import com.baeldung.dependson.config.TestConfig; |
| 29 | + |
26 | 30 | /**
|
27 | 31 | * Test class illustrating various methods of accessing a file from the classpath using Resource.
|
28 | 32 | * @author tritty
|
@@ -98,4 +102,19 @@ public void whenResourceAsFile_thenReadSuccessful() throws IOException {
|
98 | 102 | final String employees = new String(Files.readAllBytes(resource.toPath()));
|
99 | 103 | assertEquals(EMPLOYEES_EXPECTED, employees);
|
100 | 104 | }
|
| 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 | + } |
101 | 120 | }
|
0 commit comments