'How can I test resource loader in spring boot using mockito

I am working on a spring boot 2.1.3 application which has a service which uses ResourceLoader class to read a text file from the resources directory:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TestService {

  @Autowired
  ResourceLoader resourceLoader;

  public String testMethod(String test) {
    List<String> list = null;

    Resource resource = resourceLoader.getResource("classpath:test.txt");
    try (BufferedReader buffer = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
      list = buffer.lines().collect(Collectors.toList());
    } catch (Exception e) {
      System.out.println("error : " + e);
    }

    if (list.contains(test)) {
      return "in file";
    }

    return "not in file";
  }
}

I am writing a unit test for this service using mockito:

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration()
public class AServiceTest {
  @InjectMocks
  private TestService cut;

  @Test
  public void testSuccessfulResponse() {
    String actualResponse = cut.method("teststring");
    String expectedResponse = getSuccessfulResponse();

    assertThat(actualResponse, is(expectedResponse));
  }

But when I run the test resourceLoader is null?

How can I test the resourceLoader class in this example.



Solution 1:[1]

For situation like these you can simply mock & inject the resourceLoader using the @Mock & @InjectMocks annoatations and return the expected resource which is also mocked.

The key parts are:

@Mock
ResourceLoader resourceLoader;

@InjectMocks
private TestService cut;

// ... inside the test class
when(resourceLoader.getResource(anyString())).thenReturn(mockResource);

and the full code looks like this:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class TestServiceTest {

  @Mock
  ResourceLoader resourceLoader;

  @InjectMocks
  private TestService cut;

  @Test
  public void testSuccessfulResponse() throws IOException {

    // Given
    String testString = "teststring";
    InputStream is = new ByteArrayInputStream(testString.getBytes());;
    Resource mockResource = mock(Resource.class);
    when(mockResource.getInputStream()).thenReturn(is);
    when(resourceLoader.getResource(anyString())).thenReturn(mockResource);

    // When
    String actualResponse = cut.testMethod(testString);
    
    // Then
    String expectedResponse = "in file";
    assertEquals(actualResponse, expectedResponse);
  } 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ithar