mockito when-thenReturn vs doReturn-when

when(...) thenReturn(...) makes a real method call just before it is mocked. So if the called method throws an Exception you have to deal with it / mock it etc. Of course you still get your result (what you define in thenReturn(...) )

doReturn(...) when(...) does not call the method at all.

Standard HTTP Headers

RFC 2616 Fielding, et al. - 14 Header Field Definitions
NameDescription
Accept-Language
References: RFC 2616 Fielding, et al. - 14 Header Field Definitions

vi Quick Reference

Command Description
ddDelete current line
a append text after cursor
A append text at end of current line
i insert text before cursor
I insert text before first non-blank character on current line
o open new line after current line
O open new line before current line

Find Program running on a port

Sometimes you see errors like

  • Port already in use
  • bind: Address already in use
How to find what's running/using that port

On Mac
  lsof -i :80 # checks port 80

        
          COMMAND   PID USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
          java    42488   ut    6u  IPv4 0x6ddd270      0t0  TCP *:gds_db (LISTEN)

Java - Read data from URL

Sample code snippet to read data from an HTTP(S) url.
      
      public static void main(String[] args) throws Exception{

          URL url = new URL("https://www.google.com");
          URLConnection con = url.openConnection();

          BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
          String line;
          while ((line = br.readLine()) != null)
          {
              System.out.println(line);
          }
          br.close();
      }

If you are using https url, if you get the following error

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

It means that the certificate is not available. You may have to import the certificate to your JRE.
Follow the below steps:


  1. Open the url in the browser (IE/Firefox). 
  2. Export the certificate to a file. For example: to c:/youtills.crt
  3. Open command prompt navigate to the JRE folder and run the following command.
  4. .../jre> keytool -import -alias myprivateroot -keystore lib\security\cacerts -file c:\youtills.crt