Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocking UrlEncoder in a static method

I'm having trouble mocking UrlEncode.encode method which is inside a static method.

MyEncodeClass.java has this method

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

I want to force throw an exception when 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
like image 714
KamyarN Avatar asked Jun 11 '15 21:06

KamyarN


1 Answers

it is because of the version conflict between PowerMockito and Mockito artifacts. "MockCreationSettings.isUsingConstructor" method is removed in new versions, so you need to be sure you are using correct versions. e.g use PowerMockito version 1.6.2 with Mockito version 1.10.19 (this version has that missing method)

like image 145
vedat Avatar answered Sep 27 '22 17:09

vedat