繁体   English   中英

如何使用 mockito 在 spring 启动时测试资源加载器

[英]How can I test resource loader in spring boot using mockito

我正在开发一个 spring 启动 2.1.3 应用程序,该应用程序具有使用 ResourceLoader class 从资源目录读取文本文件的服务:

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";
  }
}

我正在使用 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));
  }

但是我跑测试的时候resourceLoader是null?

在此示例中,我如何测试 resourceLoader class。

我已经重新编写了您的测试。 您不应嘲笑您的TestService,因为您实际上正在对其进行测试。 这是我所做的。

  • mockFile:是代表您文件的多行字符串
  • resourceLoader:模拟并将其设置为返回Resource
  • mockResource:模拟的Resource并将其设置为返回模拟文件的InputStream。
    @Test
    public void testSuccessfulResponse() throws IOException {
        String mockFile = "This is my file line 1\nline2\nline3";
        InputStream is = new ByteArrayInputStream(mockFile.getBytes());
        cut = new TestService();
        ResourceLoader resourceLoader = Mockito.mock(ResourceLoader.class);
        cut.resourceLoader = resourceLoader;

        Resource mockResource = Mockito.mock(Resource.class);
        Mockito.when(mockResource.getInputStream()).thenReturn(is);

        Mockito.when(resourceLoader.getResource(Mockito.anyString())).thenReturn(mockResource);

        String actualResult1 = cut.testMethod("line3");
        Assert.assertEquals(actualResult1, "in file");

        String actualResult2 = cut.testMethod("line4");
        Assert.assertEquals(actualResult2, "not in file");
    }

对于像这样的情况,您可以使用@Mock@InjectMocks annoatations 简单地模拟和注入 resourceLoader 并返回同样被模拟的预期资源。

关键部分是:

@Mock
ResourceLoader resourceLoader;

@InjectMocks
private TestService cut;

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

完整的代码如下所示:

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);
  } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM