Mocking UrlEncoder in a static method

I am having problems with the mocking UrlEncode.encode method, which is inside the static method.

MyEncodeClass.java has this method

public static myEncode(String s) { UrlEncoder.encode(s, "utf-8"); } 

I want to force an exception to be thrown when the UrlEncode.encode method is called.

 @Test(expect = UnsupportedEncodingException.class) public void myTest() { PowerMockito.mockStatic(URLEncoder.class); when(URLEncoder.encode("aa", "utf-8")).thenThrow(UnsupportedEncodingException.class); MyEncodeClass.myEncode("aa"); } 

but I always get the following exception

 Caused by: java.lang.NoSuchMethodError: org.mockito.mock.MockCreationSettings.isUsingConstructor()Z 
+6
source share
1 answer

this is due to a version conflict between PowerMockito and Mockito artifacts. The "MockCreationSettings.isUsingConstructor" method is removed in new versions, so you must be sure that you are using the correct versions. for example, use PowerMockito version 1.6.2 with Mockito version 1.10.19 (this version has this missing method)

+16
source

Source: https://habr.com/ru/post/988957/


All Articles