how to mock a URL connection

前端 未结 6 977
感情败类
感情败类 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:12

    You can mock new Url instance with

    whenNew(URL.class)..
    

    Make sure you return a previously created mock object from that whenNew call.

    URL mockUrl = Mockito.mock(URL.class);
    whenNew(URL.class).....thenReturn(mockUrl );
    

    Then you can add behavior to your mock as you want.

提交回复
热议问题