Table of contents
  1. Using Files In Resources
    1. ClassPathResource
    2. Injecting a Resource using @Value
    3. Using ResourceLoader




Using Files In Resources

  • ClassPathResource

      import org.springframework.core.io.ClassPathResource;
      import org.springframework.core.io.Resource;
      import static org.assertj.core.api.Assertions.assertThat;
        
      private void loadTrackSummaryResult() throws IOException {
          Resource resource = new ClassPathResource(TRACK_SUMMARY_RESULTS_MOCK_PATH);
          try (InputStream is = resource.getInputStream()) {
              assertThat(is).isNotNull();
              trackSummaryResults = objectMapper.readValue(is, TrackSummaryResults.class);
          }
      }
    
  • Injecting a Resource using @Value

      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.core.io.Resource;
      import org.junit.jupiter.api.Test;
      import static org.assertj.core.api.Assertions.assertThat;
        
      public class TrackSummarySinkTest {
        
          @Value("classpath:your/path/to/track_summary_results_mock.json")
          private Resource trackSummaryResultsResource;
        
          @Test
          public void testLoadTrackSummaryResult() throws IOException {
              assertThat(trackSummaryResultsResource.exists()).isTrue();
              try (InputStream is = trackSummaryResultsResource.getInputStream()) {
                  trackSummaryResults = objectMapper.readValue(is, TrackSummaryResults.class);
                  // Make assertions on trackSummaryResults as needed
              }
          }
      }
    
  • Using ResourceLoader

      import org.springframework.core.io.Resource;
      import org.springframework.core.io.ResourceLoader;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import static org.assertj.core.api.Assertions.assertThat;
        
      public class TrackSummarySinkTest {
        
          @Autowired
          private ResourceLoader resourceLoader;
        
          @Test
          public void testLoadTrackSummaryResultWithResourceLoader() throws IOException {
              Resource resource = resourceLoader.getResource("classpath:your/path/to/track_summary_results_mock.json");
              assertThat(resource.exists()).isTrue();
              try (InputStream is = resource.getInputStream()) {
                  trackSummaryResults = objectMapper.readValue(is, TrackSummaryResults.class);
                  // assertions on trackSummaryResults
              }
          }
      }