如何在JUnit中创建org.springframework.http.codec.multipart.FilePart的模拟对象

huangapple go评论72阅读模式
英文:

How to create a mock object of org.springframework.http.codec.multipart.FilePart for Junit

问题

我正在尝试为一个接受类型为"org.springframework.http.codec.multipart.FilePart"的参数的函数编写单元测试用例。

然而,我无法弄清楚如何创建实现上述接口的类的模拟对象。

通过浏览一些文档,我发现了MockMultipartFile,但我无法确定是否可以使用上述对象来构造org.springframework.http.codec.multipart.FilePart类型的实例。

private boolean isValidFile(FilePart filePart){
  boolean valid = true;
  // 做一些操作
  return valid;
}

有人能帮我吗?

英文:

I am trying to write unit test case for a function which takes a parameter of type "org.springframework.http.codec.multipart.FilePart" as an input.

However I am not able to figure out how to create a mock object of the class implementing above interface.

Browsing through some documentation, I found about MockMultipartFile, however I am not able to figure out if the above object can be used to construct an instance of org.springframework.http.codec.multipart.FilePart type.

private boolean isValidFile(FilePart filePart){
  boolean valid = true;
  // Do Something
  return valid;
}

Can someone help me out please ?

答案1

得分: 2

以下是翻译好的代码部分:

最佳模拟 FilePart 的方法是

import java.io.File;
import org.junit.jupiter.api.Assertions; 
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.codec.multipart.FilePart;

import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;

class ImageCreateDtoConverterTest {

  private static final String ACCOUNT_ID = "accountId";
  private static final Integer STATUS_CODE = 1;

  private ImageCreateDtoConverter imageCreateDtoConverter;

  @BeforeEach
  void setUp() {
   this.imageCreateDtoConverter = new ImageCreateDtoConverter();
  }

  @Test
  void convert() throws Exception {

   FilePart filePart = mock(FilePart.class);
   given(filePart.filename()).willReturn("TestImage.png");

   File file = new File(filePart.filename());

   var expected = new ImageCreateDto(STATUS_CODE, ACCOUNT_ID, file);

   var actual = imageCreateDtoConverter.convert(ACCOUNT_ID, STATUS_CODE, 
       filePart);

   Assertions.assertEquals(expected, actual);
  }
}
英文:

The best way for mocking FilePart is:

import java.io.File;
import org.junit.jupiter.api.Assertions; 
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.codec.multipart.FilePart;

import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;

class ImageCreateDtoConverterTest {

  private static final String ACCOUNT_ID = "accountId";
  private static final Integer STATUS_CODE = 1;

  private ImageCreateDtoConverter imageCreateDtoConverter;

  @BeforeEach
  void setUp() {
   this.imageCreateDtoConverter = new ImageCreateDtoConverter();
  }

  @Test
  void convert() throws Exception {

   FilePart filePart = mock(FilePart.class);
   given(filePart.filename()).willReturn("TestImage.png");

   File file = new File(filePart.filename());

   var expected = new ImageCreateDto(STATUS_CODE, ACCOUNT_ID, file);

   var actual = imageCreateDtoConverter.convert(ACCOUNT_ID, STATUS_CODE, 
       filePart);

   Assertions.assertEquals(expected, actual);
  }
}

答案2

得分: 1

我已经通过我的独立接口部分来实现了。

private static class PartImpl implements Part {
    @Override
    public String name() {
        return "name";
    }

    @Override
    public HttpHeaders headers() {
        return HttpHeaders.EMPTY;
    }

    @Override
    public Flux<DataBuffer> content() {
        return DataBufferUtils.read(
            new ByteArrayResource("name".getBytes(StandardCharsets.UTF_8)), factory, 1024);
    }
}

在只是使用我实现的类进行模拟后,它按预期工作。

附:对于工厂,只需使用默认的一个。

static final DataBufferFactory factory = new DefaultDataBufferFactory();

对于 FilePart,只需按相同方式实现即可。

英文:

I was figured out implementing by my own interface Part.

private static class PartImpl implements Part {
    @Override
    public String name() {
        return &quot;name&quot;;
    }

    @Override
    public HttpHeaders headers() {
        return HttpHeaders.EMPTY;
    }

    @Override
    public Flux&lt;DataBuffer&gt; content() {
        return DataBufferUtils.read(
                new ByteArrayResource(&quot;name&quot;.getBytes(StandardCharsets.UTF_8)), factory, 1024);
    }
}

After just mock with my implemented class and it worked as expected.

P.S. For factory just use Default one

static final DataBufferFactory factory = new DefaultDataBufferFactory();

For FilePart just implement the same way.

huangapple
  • 本文由 发表于 2020年9月16日 19:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63918980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定