Campoie / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

RestAssured must use current class' classloader to load net.sf.json.AbstractJSON instead of root classloader #92

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
RestAssured fails to load AbstractJSON class when Groovy script manage 
dependencies automatically via Grape. 

In this case Grape dynamically add all necessary jars to Groovy classloader and 
then launch the script. But 
com.jayway.restassured.internal.RestAssuredResponseImpl#parseContent() uses 
root classloader to load net.sf.json.AbstractJSON, however that classloader 
know nothing about that class. So a ClassNotFoundException is thrown.

== What steps will reproduce the problem? ==

1. Install RestAssured with its dependencies in local Grape repository :

shell> grape install com.jayway.restassured rest-assured 1.2.2

If you are behind proxy add following parameters :

-Dhttp.proxyHost=proxysquid.mut -Dhttp.proxyPort=8081 -Dhttp.proxyUser=FOO 
-Dhttp.proxyPassword=BAR

To be sure that Grape successfully have downloaded all jars try the following 
command :

shell> grape resolve com.jayway.restassured rest-assured 1.2.2

2. Launch simple groovy test script with dependencies managed by Grape:

{{{
@Grab(group='com.jayway.restassured', module='rest-assured' , version='1.2.2')

import static com.jayway.restassured.RestAssured.expect

expect().statusCode(200)
.when().get("http://code.google.com/p/rest-assured/")
}}}

shell> groovy Test.groovy

== What is the expected output? What do you see instead? ==

Instead of just pass the test without any message, exception stacktrace 
appeared :

{{{
Caught: java.lang.ClassNotFoundException: net.sf.json.AbstractJSON
    at com.jayway.restassured.internal.RestAssuredResponseImpl.parseContent(RestAssuredResponseImpl.groovy:69)
    at com.jayway.restassured.internal.RestAssuredResponseImpl.this$2$parseContent(RestAssuredResponseImpl.groovy)
    at com.jayway.restassured.internal.RestAssuredResponseImpl.parseResponse(RestAssuredResponseImpl.groovy:39)
    at com.jayway.restassured.internal.ResponseSpecificationImpl$_HamcrestAssertionClosure_getClosure_closure1.doCall(ResponseSpecificationImpl.groovy:312)
    at com.jayway.restassured.internal.RequestSpecificationImpl$1.super$2$doRequest(RequestSpecificationImpl.groovy)
    at com.jayway.restassured.internal.RequestSpecificationImpl$1.doRequest(RequestSpecificationImpl.groovy:483)
    at com.jayway.restassured.internal.RequestSpecificationImpl.sendHttpRequest(RequestSpecificationImpl.groovy:533)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendHttpRequest(RequestSpecificationImpl.groovy)
    at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:527)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendRequest(RequestSpecificationImpl.groovy)
    at com.jayway.restassured.internal.filter.RootFilter.filter(RootFilter.groovy:28)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:45)
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeFilterChain(RequestSpecificationImpl.groovy:459)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:672)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy)
    at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:102)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.get(ResponseSpecificationImpl.groovy:208)
    at RecupererDevisTest.run(Test.groovy:46)
}}}

One can make script work by adding json-lib and commons-lang (json-lib's 
dependency) to the java classpath :

> groovy -cp json-lib-2.3-jdk15.jar;commons-lang-2.4.jar Test.groovy

== What version of the product are you using? On what operating system? ==

Tested with RestAssured 1.2.2 and Groovy 1.8.0

== Please provide any additional information below. ==

RestAssured should do this :

Class.forName("net.sf.json.AbstractJSON", true, 
this.getClass().getClassLoader())

instead of 

Class.forName("net.sf.json.AbstractJSON")

Original issue reported on code.google.com by voldemar...@gmail.com on 19 Aug 2011 at 5:04

Attachments:

GoogleCodeExporter commented 9 years ago
And even better is use current thread context classloader :

Class.forName("net.sf.json.AbstractJSON", true, 
Thread.currentThread().getContextClassLoader())

Original comment by voldemar...@gmail.com on 20 Aug 2011 at 3:44

GoogleCodeExporter commented 9 years ago
Thanks for reporting and for the detailed description. Will apply your fix soon.

Original comment by johan.ha...@gmail.com on 20 Aug 2011 at 4:37

GoogleCodeExporter commented 9 years ago

Original comment by johan.ha...@gmail.com on 30 Aug 2011 at 11:55

GoogleCodeExporter commented 9 years ago
The class is now loaded by the threads context CL. Please verify if you like. A 
snapshot of RA has been uploaded to 
https://oss.sonatype.org/content/repositories/snapshots/.

Original comment by johan.ha...@gmail.com on 4 Sep 2011 at 5:02