testing powermock simulate http server time out for client call

前端 未结 2 952
暖寄归人
暖寄归人 2021-01-28 07:41

i need to write testcase for connectTimeout and SocketTimeout exception. Am using powerMock to create mock objects. Below is my code. But am getting null pointer exception for m

相关标签:
2条回答
  • 2021-01-28 08:01

    Finally i was able to solve it. Incase some gets the same issue again. In my code issue was withArguments line. I was trying to pass

    String url="https://www.google.co.in/";

    as the Url parameter. But in the actual it was null.

    URL mockURL = PowerMockito.mock(URL.class); PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(mockURL);

    withArguments try to match if the parameters are same. On having same url paramter on my mock and actual object in code i was able to proceed with out null pointer exception

    0 讨论(0)
  • 2021-01-28 08:07

    Acording to powermock documentation, in the section mock constructor, you should be doing something like that:

    URL mockURL = PowerMockito.mock(URL.class);
    expectNew(File.class, url).andReturn(mockURL);
    replay(mockURL, File.class);
    

    Also, be sure to use annotations @PrepareForTest(Sender.class) and @RunWith(PowerMockRunner.class)

    0 讨论(0)
提交回复
热议问题