how to mock a URL connection

前端 未结 6 995
感情败类
感情败类 2020-12-06 01:50

Hi I have a method that takes an URL as an input and determines if it is reachable. Heres the code for that:

public static boolean isUrlAccessible(final Str         


        
6条回答
  •  广开言路
    2020-12-06 02:28

    It's much simpler with the JMockit mocking API (and even simpler with no mocking):

    import java.io.*;
    import java.net.*;
    import org.junit.*;
    import static org.junit.Assert.*;
    import mockit.*;
    
    public final class ExampleURLTest {
       public static final class ClassUnderTest {
          public static boolean isUrlAccessible(String urlToValidate) throws IOException {
             HttpURLConnection huc = null;
             int responseCode;
    
             try {
                URL url = new URL(urlToValidate);
                huc = (HttpURLConnection) url.openConnection();
                huc.setRequestMethod("HEAD");
                huc.connect();
                responseCode = huc.getResponseCode();
             }
             finally {
                if (huc != null) {
                   huc.disconnect();
                }
             }
    
             return responseCode == 200;
          }
       }
    
       // Proper tests, no unnecessary mocking ///////////////////////////////////////
    
       @Test
       public void checkAccessibleUrl() throws Exception {
          boolean accessible = ClassUnderTest.isUrlAccessible("http://google.com");
    
          assertTrue(accessible);
       }
    
       @Test(expected = UnknownHostException.class)
       public void checkInaccessibleUrl() throws Exception {
          ClassUnderTest.isUrlAccessible("http://inaccessible12345.com");
       }
    
       @Test
       public void checkUrlWhichReturnsUnexpectedResponseCode(
          @Mocked URL anyURL, @Mocked HttpURLConnection mockConn
       ) throws Exception {
          new Expectations() {{ mockConn.getResponseCode(); result = -1; }};
    
          boolean accessible = ClassUnderTest.isUrlAccessible("http://invalidResource.com");
    
          assertFalse(accessible);
       }
    
       // Lame tests with unnecessary mocking ////////////////////////////////////////
    
       @Test
       public void checkAccessibleUrl_withUnnecessaryMocking(
          @Mocked URL anyURL, @Mocked HttpURLConnection mockConn
       ) throws Exception {
          new Expectations() {{ mockConn.getResponseCode(); result = 200; }};
    
          boolean accessible = ClassUnderTest.isUrlAccessible("http://google.com");
    
          assertTrue(accessible);
       }
    
       @Test(expected = UnknownHostException.class)
       public void checkInaccessibleUrl_withUnnecessaryMocking(
          @Mocked URL anyURL, @Mocked HttpURLConnection mockConn
       ) throws Exception {
          new Expectations() {{ mockConn.connect(); result = new UnknownHostException(); }};
    
          ClassUnderTest.isUrlAccessible("http://inaccessible12345.com");
       }
    }
    

    (Verified with JMockit 1.47 on JDKs 8 & 9.)

提交回复
热议问题