0

im working in a spring boot projet where i create an api that return a file from classpath and all works fine , this is my api :

@Value(value = "classpath:pdf/notice_file.pdf")
private Resource myPdfResource;

@GetMapping("/getLocalDocument/{typeDoc}")
public ResponseEntity<byte[]> getLocalDocument(@PathVariable String typeDoc)
{
    byte[] contents = new byte[0];

    HttpStatus status = HttpStatus.OK;
    HttpHeaders headers = new HttpHeaders();

    final InputStream in;
    String filename="";

    try {

        headers.setContentType(MediaType.APPLICATION_PDF);

        if ("NOTICE".eqauls(typeDoc)) {
            in = myPdfResource.getInputStream();
            contents = IOUtils.toByteArray(in);
            filename = "notice_1.pdf";
        }

        headers.setContentDispositionFormData(filename, filename);
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");

        return new ResponseEntity<>(contents, headers, status);

    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

all the tests methods are ok but i'm getting an error with the method getLocalDocumentTest , and this is my unit test code :

@RunWith(SpringRunner.class)
@PrepareForTest(IOUtils.class)
public class ApiTest{

    @Mock
    private Resource myPdfResource;

    @InjectMocks
    private Api api;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(api).build();
    }

    @Test
    public void getLocalDocumentTest() throws Exception {

        String typeDoc= "NOTICE";
        byte[] contents = new byte[0];
         InputStream stubInputStream = IOUtils.toInputStream("some test data for my input stream", "UTF-8");;

        String URI = "/v1/getLocalDocument/"+typeDoc;

        when(myPdfResource.getInputStream()).thenReturn(stubInputStream);

        PowerMockito.mockStatic(IOUtils.class);
        PowerMockito.when(IOUtils.toByteArray(any(InputStream.class))).thenReturn(contents);

        RequestBuilder requestBuilder = MockMvcRequestBuilders.get(URI);

        MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
        MockHttpServletResponse response = mvcResult.getResponse();

        assertEquals(HttpStatus.OK.value(), response.getStatus());
    }

}

when i run the test i'm getting the following error :

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().

do you have any idea please why i'm getting this error , im new in mockito framework

Thanks.

Boendal
  • 2,496
  • 1
  • 23
  • 36
James
  • 1,190
  • 5
  • 27
  • 52
  • Why are you mocking (`mockStatic`) `IOUtils` when you have it initialized above? – amer Jan 30 '20 at 10:23
  • thank you for you time , i added it according to this response https://stackoverflow.com/questions/25258876/mocking-apache-commons-io-ioutils-class even i remove the mockstatic i get the same error – James Jan 30 '20 at 10:31
  • Remove the mock too , then try , if it still shows the error then the problem is with mocking of @Value... check this link too https://stackoverflow.com/questions/23162777/how-do-i-mock-an-autowired-value-field-in-spring-with-mockito – amer Jan 30 '20 at 10:35
  • @amer not working :( – James Jan 30 '20 at 13:38

0 Answers0