0

I have 1 java class which is rendering URL as PDF , below is the sample code:

public byte[] renderURLAsPDF(final String url, final Long id) throws SomeException {
    
    try {
        URLConnection connection = new URL(url).openConnection();
   
        connection.setRequestProperty(HttpHeaders.USER_AGENT, "");

        try (InputStream openStream = connection.getInputStream()) {
            PDFVerifierInputStream pdfInputStream = new PDFVerifierInputStream(openStream);
            return IOUtils.toByteArray(pdfInputStream);
        }

    } catch (FileNotFoundException ex) {
      
        throw SomeException.newInstanceWithHttpNotFound("Failed to find a PDF file for Booking Ref : " + itineraryItemId );
    } catch (IOException e) {
        throw new OrionException("Failed to render URL As PDF for Booking Ref: " + itineraryItemId, e);
    }
}

So I want to create 1 test for this class which will throw FileNotFoundException.

What I have done to test this method I created mocked connection but it am not able to throw that FileNotFoundException from inputStream.getInputStream() code which i created.

public void renderURLAsPDFFileNotFoundException() throws Exception {
    URLConnection urlConnection = mock(URLConnection.class);
    
    URLStreamHandler stubUrlHandler = new URLStreamHandler() {
        @Override
        protected URLConnection openConnection(URL u) throws IOException {
            return urlConnection;
        }
    };
    
    String url = "https://myurl.com";
    IOException fileNotFoundException = new FileNotFoundException(url);
    given(urlConnection.getInputStream()).willThrow(fileNotFoundException);
    SomeException exception = assertThrows(SomeException.class, () -> ticketPDFGateway.renderURLAsPDF(url, ITEM_ID));
    assertEquals(HttpStatus.NOT_FOUND, exception.getHttpStatus());
}

Actaully I am new to mockito, I mocked the connection but it is still throwing IOException, so Can anyone help me on this how can I properly mock the connection so Java class will know the mocked connection for the test class.

Diego Borba
  • 1,282
  • 8
  • 22
jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
  • Where does `URLStreamHandler` come into play? I don't see it used anywhere in your production code – knittl Aug 08 '23 at 19:08
  • Does this answer your question? [Why are my mocked methods not called when executing a unit test?](https://stackoverflow.com/questions/74027324/why-are-my-mocked-methods-not-called-when-executing-a-unit-test) – knittl Aug 08 '23 at 19:09
  • @knittl you can remove URLStreamHandler and you can directly have openConnection it will also not work and that Url which you mentioned in chat , it is not useful for my case – jitendra varshney Aug 10 '23 at 06:28
  • Why is not helpful? You are calling `new URL` in your service method and your `urlConnection` mock is never used anywhere by your real code, it only exists in your test method and nowhere else. The linked question (which chat?) explains the same problem and provides several solutions how to resolve the problem by restructuring your code. – knittl Aug 10 '23 at 06:33

0 Answers0