Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Need help with Junit for AEM code wherein we make a rest call and retrieve data

Avatar

Level 9

Hi All,

Code is as below :

public String execute(String url)

{

try{

HttpClient httpClient = new HttpClient();

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(this.timeOut);

getMethod = new GetMethod(url);

String encoding = Base64.encode(this.name + ":" + this.password);

getMethod.setRequestHeader("Authorization", "Basic " + encoding);

int code = httpClient.executeMethod(getMethod);

....and so on.

}

The control is coming[when run as Junit test] till the line "getMethod.setRequestHeader("Authorization", "Basic " + encoding);" along  with the error java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/net/SocketFactory" and not going beyond it. Googled out and have been trying multiple options for the past one day, but no luck.

Any thoughts/pointers on how to solve this will be helpful.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

a) PowerMock is evil, try to use Mockito as much as you can.

b) Having Junit test to rely on external(as I presume REST call) makes your unit tests flaky. Junit tests are supposed to work with your class code, not external system calls.

c) Better move it as an external service and inject it's Interface as a mock to test it.

View solution in original post

5 Replies

Avatar

Administrator

Hi

Java.lang.LinkageError occurs when there is a problem in ClassLoading. Actually, this happens when same class is loaded by multiple class loader. As you know, in OSGI, Each bundle has their own class loader. 

Below post could guide you more on this. Also, check all your POM.xml and make sure, there are no circular dependencies between bundles.

Link:- http://stackoverflow.com/questions/244482/how-to-deal-with-linkageerrors-in-java

Old Forum Post:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Correct answer by
Community Advisor

a) PowerMock is evil, try to use Mockito as much as you can.

b) Having Junit test to rely on external(as I presume REST call) makes your unit tests flaky. Junit tests are supposed to work with your class code, not external system calls.

c) Better move it as an external service and inject it's Interface as a mock to test it.

Avatar

Level 9

Hi Kautuk/PuzanovsP,

Thank you for your replies. 

@PuzanovsP : You have mentioned that using PowerMock is not a recommended approach. I do not have much insight into this. Any thoughts on why it is not recommended would be helpful.

Avatar

Level 9

Hi PuzanovsP,

Thank you for your reply.