编写用于多部分文件的单元测试?

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

Writing unit test for multipart file?

问题

如何为Multipart文件编写单元测试用例?

@Data
Class Document { 
    String name;
    byte[] byteFile;
}
@Service 
public class DocService {
   
  public Document makeDocument(Multipart file, String name) {
     Document document = makeDocument(file, name);
     return document;
  }
  
  private Document document() {
      Document document = new Document();
      document.setName(name);
      document.setByteFile(file.getbyte());
      return document;
  }
}
MockMultipartFile file =
                new MockMultipartFile("file","test contract.pdf",MediaType.APPLICATION_PDF_VALUE,
                        "<<pdf data>>".getBytes(StandardCharsets.UTF_8));
                       
                        
@Test
void testMakeDocument() {
    when(docService.makeDocument(file, "any")).thenReturn(document);
    assertNotNull();//
    assertEquals();
}

出现此错误:
when() 需要一个参数,该参数必须是“模拟调用的方法”。

when(docService.makeDocument(file, "any")).thenReturn(document);

如何为此编写测试用例?

尝试了各种方法,例如使用

@Mock
Multipart file

但然后会抛出“document cannot be returned by getbytes() getbyte() should be return byte[]”错误。

如何编写单元测试用例?

英文:

How to write unit test case for Multipart file?

@Data
Class Document { 
    String name;
    byte[] byteFile;
}
@Service 
public class DocService {
   
  public Document makeDocument(Multipart file, String name) {
     Document document = makeDocument(file, name);
     return document;
  }
  
  private Document document() {
      Document document = new Document();
      document.setName(name);
      document.setByteFile(file.getbyte());
      return document;
  }
}
MockMultipartFile file =
                new MockMultipartFile(&quot;file&quot;,&quot;test contract.pdf&quot;,MediaType.APPLICATION_PDF_VALUE,
                        &quot;&lt;&lt;pdf data&gt;&gt;&quot;.getBytes(StandardCharsets.UTF_8));
                       
                        
@test
void testMakeDocument() {
    when(docService.makeDocument(file, &quot;any&quot;)).thenReturn(document);
    assertNotNull();//
    assertEquals();
}

Getting this error :
when() requires an argument which has to be 'a method call on a mock'.

when(docService.makeDocument(file, &quot;any&quot;)).thenReturn(document);

How to write test case for this ?

Tried various approach like using

@Mock
Multipart file

but then throws document cannot be returned by getbytes() getbyte() should be return byte[].

How to write unit test case ?

答案1

得分: 0

以下是您要翻译的内容:

"After a couple of fixes in your code ( the updated code is included ) an approach to test it is the following :

public class DocServiceTest {
    @Test
    public void testMakeDocument() {

        Multipart mMultipart = mock(Multipart.class);
        final byte[] bytes = {1, 2, 3, 4};
        when(mMultipart.getbyte()).thenReturn(bytes);

        final DocService docService = new DocService();

        final Document aTest = docService.makeDocument(mMultipart, &quot;aTest&quot;);
        assertNotNull(aTest);

        assertEquals(&quot;aTest&quot;, aTest.getName());
        assertEquals(bytes, aTest.getByteFile());
    }
}

As for your updated code :

@Service
public class DocService {
    public Document makeDocument(Multipart file, String name) {
        return document(file, name);
    }

    private Document document(Multipart file, String name) {
        Document document = new Document();
        document.setName(name);
        document.setByteFile(file.getbyte());
        return document;
    }
}
@Data
public class Document {
    String name;
    byte[] byteFile;
}

This is included as it applies to any "Multipart" you are going to test. As you did not have an include to use the same an interface is placed to substitute it.

public interface Multipart {
    byte[] getbyte();
}
```"

<details>
<summary>英文:</summary>

After a couple of fixes in your code ( the updated code is included ) an approach to test it is the following : 

```java
public class DocServiceTest {
    @Test
    public void testMakeDocument() {

        Multipart mMultipart = mock(Multipart.class);
        final byte[] bytes = {1, 2, 3, 4};
        when(mMultipart.getbyte()).thenReturn(bytes);

        final DocService docService = new DocService();

        final Document aTest = docService.makeDocument(mMultipart, &quot;aTest&quot;);
        assertNotNull(aTest);

        assertEquals(&quot;aTest&quot;, aTest.getName());
        assertEquals(bytes, aTest.getByteFile());
    }
}

As for your updated code :

@Service
public class DocService {
    public Document makeDocument(Multipart file, String name) {
        return document(file, name);
    }

    private Document document(Multipart file, String name) {
        Document document = new Document();
        document.setName(name);
        document.setByteFile(file.getbyte());
        return document;
    }
}
@Data
public class Document {
    String name;
    byte[] byteFile;
}

This is included as it applies to any "Multipart" you are going to test. As you did not have an include to use the same an interface is placed to substitute it.

public interface Multipart {
    byte[] getbyte();
}

huangapple
  • 本文由 发表于 2023年5月31日 23:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374880.html
匿名

发表评论

匿名网友

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

确定