6

So I have recently started facing TestNGException while trying to run our existing test suite using the IDE. By recently I mean updating the intelliJ and dependencies to try working on latest versions. The code and the stack-trace for failure is as follows -

Code Usage

ITests.java -

public class ITests extends BaseTest {
    private final Action action = new Action();
}

BaseTest (just to share the syntax)-

public class BaseTest {
    // where in this class does not use any instance of Http
}

Action.java -

public class Action {
    private final Http http = Http.getInstance();
}

Http.java -

public class Http {

    private static final Http INSTANCE = new Http(); //line 36

    public static Http getInstance() {
        return INSTANCE;
    }

    private Http() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); //line 43
        connectionManager.setMaxTotal(100);
        connectionManager.setDefaultMaxPerRoute(100);
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(60000)
                .setConnectTimeout(60000)
                .setConnectionRequestTimeout(60000)
                .build();
        this.client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig)
                .setConnectionManager(connectionManager).build();
    }
}

Maven Dependency -

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

StackTrace -

org.testng.TestNGException: 
Cannot instantiate class com.package.tests.test.ITests
  at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
  at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:382)
  at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:295)
  at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:118)
  at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:183)
  at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:128)
  at org.testng.TestRunner.initMethods(TestRunner.java:416)
  at org.testng.TestRunner.init(TestRunner.java:242)
  at org.testng.TestRunner.init(TestRunner.java:212)
  at org.testng.TestRunner.<init>(TestRunner.java:159)
  at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:549)
  at org.testng.SuiteRunner.init(SuiteRunner.java:161)
  at org.testng.SuiteRunner.<init>(SuiteRunner.java:114)
  at org.testng.TestNG.createSuiteRunner(TestNG.java:1290)
  at org.testng.TestNG.createSuiteRunners(TestNG.java:1277)
  at org.testng.TestNG.runSuitesLocally(TestNG.java:1131)
  at org.testng.TestNG.run(TestNG.java:1048)
  at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
  at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
Caused by: java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
  ... 18 more
Caused by: java.lang.NoSuchMethodError: org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:176)
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:158)
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:125)
  at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:116)
  at com.package.core.Http.<init>(Http.java:43)
  at com.package.core.Http.<clinit>(Http.java:36)
  at com.package.tests.action.Action.<init>(Action.java:16)
  at com.package.tests.test.ITests.<init>(ITests.java:24)
  ... 23 more

Note - Our project is base on maven, so while I execute the tests using maven exec:java the execution goes fine.

Please do ask for any other info required.

Edit 1 -

As asked by @hunter, debugging to get the exact path of Cpool from where it loaded. Evaluated -

Class.forName("org.apache.http.impl.conn.CPool").getResource‌​("CPool.class")

Result -

file:/Users/xyz/.m2/repository/org/apache/httpcomponents/httpclient/4.5.2/httpclient-4.5.2.jar!/org/apache/http/impl/conn/CPool.class

Edit 2 -

mvn dependency:tree [most related to httpcomponents] -

--- maven-dependency-plugin:2.8:tree

com.package:core:jar:1.1.3

+- org.apache.httpcomponents:httpclient:jar:4.5.2:compile |  +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile |  +- commons-logging:commons-logging:jar:1.2:compile |  \- commons-codec:commons-codec:jar:1.9:compile
+- com.google.collections:google-collections:jar:1.0:compile
+- commons-io:commons-io:jar:2.5:compile
+- org.apache.commons:commons-lang3:jar:3.4:compile
+- org.json:json:jar:20160810:compile \- org.testng:testng:jar:6.9.9:compile

com.package:test:jar:1.1.3

+- mysql:mysql-connector-java:jar:6.0.3:compile
+- org.apache.httpcomponents:httpclient:jar:4.5.2:compile |  +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile |  +- commons-logging:commons-logging:jar:1.2:compile |  \- commons-codec:commons-codec:jar:1.9:compile
+- com.package:mobile:jar:3.6.2:compile |  +- com.pkg.serviceproxy:http-handler:jar:1.7.41:compile |  |  +- org.apache.httpcomponents:httpcore-nio:jar:4.3:compile |  +- com.pkg:bullseye-model:jar:1.2.0:compile |  |  +- com.codahale.metrics:metrics-httpclient:jar:3.0.2:compile |  +- kpg:pz-api:jar:0.2.38:compile |  |  \- com.mashape.unirest:unirest-java:jar:1.4.7:compile |  |     +- org.apache.httpcomponents:httpasyncclient:jar:4.0.2:compile |  |     \- org.apache.httpcomponents:httpmime:jar:4.3.6:compile
+- com.package:core:jar:1.1.3:compile

com.package:driver:jar:1.1.3

\- com.package:test:jar:1.1.3:compile

Edit 3 - intelliJ IDEA - 15.0.3 [Community] - updated on 19th Jan,2016

Edit 4 -

Bending more towards intelliJ, sharing the logs post running the tests using testng.xml -> right click -> run

> objc[21590]: Class JavaLaunchHelper is implemented in both
> /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java
> and
> /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/libinstrument.dylib.
> One of the two will be used. Which one is undefined.
> [AppClassLoader@14dad5dc] error can't determine annotations of missing
> type javax.cache.annotation.CachePut when weaving type
> com.intellij.rt.execution.application.AppMain when weaving classes 
> when weaving   [Xlint:cantFindType] [AppClassLoader@14dad5dc] error
> can't determine annotations of missing type
> javax.cache.annotation.CacheResult when weaving type
> com.intellij.rt.execution.application.AppMain when weaving classes 
> when weaving   [Xlint:cantFindType] [AppClassLoader@14dad5dc] error
> can't determine annotations of missing type
> javax.cache.annotation.CacheRemove when weaving type
> com.intellij.rt.execution.application.AppMain when weaving classes 
> when weaving   [Xlint:cantFindType] [AppClassLoader@14dad5dc] error
> can't determine annotations of missing type
> javax.cache.annotation.CacheRemoveAll when weaving type
> com.intellij.rt.execution.application.AppMain when weaving classes 
> when weaving   [Xlint:cantFindType]
Naman
  • 27,789
  • 26
  • 218
  • 353
  • debug the test and put a break point first line Http constructor, and when break point hits evaluate this expression Class.forName("org.apache.http.impl.conn.CPool").getResource("CPool.class") – hunter Sep 02 '16 at 10:30
  • @hunter - did that, what are we trying to achieve with that(read - what info is reqd. post that?) – Naman Sep 02 '16 at 10:41
  • it will display where the class Cpool has been loaded from, normally it should be from your local maven repo.but we can verify weather idea run-time refers to the correct jar . it should display a path, pls update the post with that result – hunter Sep 02 '16 at 11:02
  • @hunter - updated the post, the class is from my m2 only. detailed path in the post – Naman Sep 02 '16 at 12:00
  • tro try to evaluate following expression, Class.forName("org.apache.http.pool.AbstractConnPool").getResource("AbstractConnPool.class") , if it displays a httpcore version less than 4.4 , then it is incompatible with httpclient 4.2.5 . it might be a dependency reolving problem in IDEA maven plugin. – hunter Sep 02 '16 at 12:23
  • Could you share the result of `mvn dependency:tree`? – juherr Sep 02 '16 at 12:30
  • @JulienHerr any specific dependency(ies) you are interested in? – Naman Sep 02 '16 at 12:31
  • dependency tree will not help because this works in mvn command line , right? – hunter Sep 02 '16 at 12:32
  • @hunter - evaluates to `null`.. dependency versions and resolutions is what Julien might be worried about I guess – Naman Sep 02 '16 at 12:36
  • I didn't catch it is working well outside the ide. – juherr Sep 02 '16 at 12:36
  • it should not be null, something might be wrong in the expression i have given, anyway i am sure that this setValidateAfterInactivity method was introduced after version 4.4 of httpcore artifact, IDEA resolve a lower version of httpcore due to some reason,pls try what i mentioned in my answer – hunter Sep 02 '16 at 12:39
  • In fact, I'm pretty sure it is a classpath issue where idea and maven don't manage the order of the classpath in the same way. – juherr Sep 02 '16 at 12:45
  • @JulienHerr - updated the post with relative httpcomponents from all the packages used. Do call out in case of any specific suspicion. – Naman Sep 02 '16 at 13:04
  • `org.apache.httpcomponents:httpcore:jar:4.4.4:compile` looks bad! Just try to remove it from our dependencies. – juherr Sep 02 '16 at 13:21
  • @JulienHerr that is coming from `httpcomponents:httpclient 4.5.2` itself – Naman Sep 02 '16 at 14:41
  • True... You can try something like http://stackoverflow.com/q/11577079/4234729 – juherr Sep 02 '16 at 14:53
  • @JulienHerr - tried using http://www.programmers-pain.de/2011/02/28/maven-duplicate-finder-plugin-find-duplicate-classes-in-maven-artefacts/ ... but no build errors, meaning no duplicate classes either – Naman Sep 02 '16 at 15:31
  • No more idea on my side. Are you able to share a minimal project which shows the issue ? – juherr Sep 02 '16 at 15:43
  • @JulienHerr - apologies, that wouldn't be feasible on my end either. – Naman Sep 02 '16 at 15:53
  • @JulienHerr - a final look at Edit 4, might help us maybe. – Naman Sep 02 '16 at 16:26
  • Nope, it doesn't help, sorry. Did you try on another computer/os? As it looks like an idea issue, you should open an issue on it. But they will have to reproduce too and it is headache without a runnable project – juherr Sep 02 '16 at 17:10

1 Answers1

3

In IDEA, Maven project tab, click on the Show dependencies button (it will show a diagram) , then press Ctrl+ F in that diagram and type httpcore and check the version. if it shows the version less than 4.4 , you can analyze how dependencies has been resolved. Based on that you can adjust the pom to rectify the dependencies. Since it is working with mvn in the command line, this might be a incorrect behavior of the that IDEA resolve maven dependencies through its maven plugin.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
hunter
  • 3,963
  • 1
  • 16
  • 19
  • `+- org.apache.httpcomponents:httpclient:jar:4.5.2:compile | +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile` from the tree – Naman Sep 02 '16 at 12:44
  • this versions are compatible, is this the output from mvn dependency:tree or output from IDEA dependency diagram? we cannot troubleshoot this using mvn dependency:tree – hunter Sep 02 '16 at 12:47
  • in IDEA hit Ctrl+N , then type AbstractConnPool browse to the class, now it will show you what is the jar that IDEA referes (in the Window titile) – hunter Sep 02 '16 at 12:51
  • sorry but this isn't helping – Naman Sep 08 '16 at 05:33
  • have you checked the versions in idea dependency diagram? – hunter Sep 11 '16 at 04:27
  • its shared in the post as well – Naman Sep 12 '16 at 05:33
  • no, i think you have shared the mvn dependency:tree, select the maven project in idea (in IDEA maven tab) and hot ctrl+shift+alt+U, this will generate the diagrame, lookup for the artifacts and its versions – hunter Sep 12 '16 at 05:51
  • just a suggestion, not everyone uses the same IDE/settings as yours, please look for better generalised steps while you direct to some settings. – Naman Sep 12 '16 at 06:09
  • same goes for the answer as well – Naman Sep 12 '16 at 06:09