尝试运行此代码时出现以下错误:

import com.jayway.restassured.response.Response;
import static com.jayway.restassured.RestAssured.*;

public class TestRestAssured {
public static void main (String args[]){
// Call the REST service
Response res = get("http://10.213.75.170:5000/bla"); // <--- Crashes on this line.

    // Get the JSON payload
    String json = res.asString();
 }

   }




java.lang.NullPointerException
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$DgmMethodRecord.loadDgmInfo(GeneratedMetaMethod.java:163)
    at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.registerMethods(MetaClassRegistryImpl.java:155)
    at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:83)
    at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:61)
    at groovy.lang.GroovySystem.<clinit>(GroovySystem.java:29)
    at org.codehaus.groovy.runtime.InvokerHelper.<clinit>(InvokerHelper.java:49)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.createMap(ScriptBytecodeAdapter.java:619)
    at com.jayway.restassured.internal.ResponseParserRegistrar.<init>(ResponseParserRegistrar.groovy)
    at com.jayway.restassured.RestAssured.<clinit>(RestAssured.java:336)
    at TestRestAssured.main(TestRestAssured.java:16)
Exception in thread "main" java.lang.NullPointerException
    at org.codehaus.groovy.runtime.callsite.CallSiteClassLoader.loadClass(CallSiteClassLoader.java:45)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.loadClass(ClassLoaderForClassArtifacts.java:58)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.define(ClassLoaderForClassArtifacts.java:42)
    at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:86)
    at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:84)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.defineClassAndGetConstructor(ClassLoaderForClassArtifacts.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteGenerator.compilePojoMethod(CallSiteGenerator.java:229)
    at org.codehaus.groovy.reflection.CachedMethod.createPojoMetaMethodSite(CachedMethod.java:257)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.createCachedMethodSite(PojoMetaMethodSite.java:159)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.createPojoMetaMethodSite(PojoMetaMethodSite.java:148)
    at groovy.lang.MetaClassImpl.createPojoCallSite(MetaClassImpl.java:3015)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPojoSite(CallSiteArray.java:116)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:150)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at com.jayway.restassured.internal.ResponseParserRegistrar.<init>(ResponseParserRegistrar.groovy:42)
    at com.jayway.restassured.RestAssured.createTestSpecification(RestAssured.java:1094)
    at com.jayway.restassured.RestAssured.given(RestAssured.java:746)
    at com.jayway.restassured.RestAssured.get(RestAssured.java:776)
    at TestRestAssured.main(TestRestAssured.java:16)


我使用的是放心的1.7,常规的1.8.8,所有放心的依赖项都已加载到我的系统库路径中。我也在使用jdk 1.7。作为同事,这是某种环境问题,因为使用相同的.jar文件运行相同的代码没有问题...

最佳答案

那么您的get似乎返回了NullPointerException

at com.jayway.restassured.RestAssured.get(RestAssured.java:776)


该方法内部出现问题。

您可能错过了instructions中提到的第三方依赖性。加载其他类时,似乎弹出错误。


非Maven用户

下载REST Assured,并将其放在您的类路径中。您可能还需要下载第三方依赖项,并将它们也放入您的类路径中。

07-28 14:08