0

I want to do the unit test for the next function that it is working with Postman.

@PostMapping("/uploadGText")
  public ResponseEntity<Object> translateGt(@RequestParam("text") MultipartFile file, 
                          @RequestParam("langI") String langI,
                          @RequestParam("langO") String langO) throws IOException{
    
    String fileName = fileStorageService.storeFile(file);
    String path = "Uploads\\"+fileName;
  
    TxtFile tFile = new TxtFile();
    tFile.getPath(path,langI,langO);
                              
    return downloadFile(tFile.getNewPath());
  }

this is the unit test at the moment, i want to send the parameters and realize an asserEquals

@Test
public void shouldExecuteTranslateController() throws IOException {

    Path path = Paths.get("Uploads\\testText.txt");
    String name = "text";
    String originalFileName = "testText.txt";
    String contentType = "text/plain";
    byte[] content = null;
    try {
        content = Files.readAllBytes(path);
    } catch (final IOException e) {
    }
    MultipartFile result = new MockMultipartFile(name,
            originalFileName, contentType, content);
    TranslateController tr = new TranslateController();
    tr.translateGt(result,"en","es");
    //assertEquals("textTet", tr.translateGt(result,"en","es");)
}

This is the error message I think the problem is the MultiPartFile.


java.lang.NullPointerException
    at com.jalasoft.convert.controller.endpoint.TranslateController.translateGt(TranslateController.java:32)
    at com.jalasoft.convert.controller.endpoint.TranslateControllerTest.shouldExecuteTranslateController(TranslateControllerTest.java:34)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at 
xerx593
  • 12,237
  • 5
  • 33
  • 64
  • Hi & Welcome! *Probably*: `fileStorageService` is null!? ;) ... -> https://spring.io/guides/gs/testing-web/ – xerx593 Oct 28 '22 at 03:18
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – 1615903 Oct 28 '22 at 03:38
  • Suspected duplicate: https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null – chrylis -cautiouslyoptimistic- Oct 28 '22 at 04:02

0 Answers0