在JUnit中获取ExceptioninitializerError

时间:2017-08-20 07:50:34

标签: java eclipse junit

以下是我在运行JUnit测试用例时遇到的错误。

在运行SV的JUnit测试用例作为代码时,我得到以下错误,即使所有SV都是代码罐。代码用于创建虚拟服务。如果我删除了行" forGet(URL)......;"测试用例会通过,否则会抛出错误。

java.lang.ExceptionInInitializerError
at com.ca.svcode.protocols.http.agent.HttpAgentProtocol.getInterceptor(HttpAgentProtocol.java:77)
at com.ca.svcode.protocols.http.agent.HttpAgentProtocol.getInterceptor(HttpAgentProtocol.java:48)
at com.ca.svcode.engine.IpvConnectedServer.start(IpvConnectedServer.java:135)
at com.ca.svcode.engine.IpvProtocolServer.withConnection(IpvProtocolServer.java:67)
at com.ca.svcode.protocols.http.fluent.impl.HttpTransactionBuilderImpl.buildTransaction(HttpTransactionBuilderImpl.java:77)
at com.ca.svcode.protocols.http.fluent.AbstractHttpTransactionBuilder.doReturn(AbstractHttpTransactionBuilder.java:132)
at TestClass.testSimpleHttpGetWithResponseCodeAndStringBody(TestClass.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.RuntimeException: Cannot load initialize agent with HTTP Protocol.
at com.ca.svcode.protocols.http.agent.HttpInterceptor.<clinit>(HttpInterceptor.java:45)
... 32 more
Caused by: com.ca.svcode.agent.AgentException: Failed to initialize agent for protocol
at com.ca.svcode.agent.Agent.initialize(Agent.java:76)
at com.ca.svcode.protocols.http.agent.HttpInterceptor.<clinit>(HttpInterceptor.java:43)
... 32 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at com.ca.svcode.agent.AgentLoader.loadAgent(AgentLoader.java:62)
at com.ca.svcode.agent.Agent.initialize(Agent.java:70)
... 33 more
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.ca.svcode.agent.AgentLoader.dynamicLoadOfVirtualMachine(AgentLoader.java:87)
at com.ca.svcode.agent.AgentLoader.loadAgent(AgentLoader.java:60)
... 34 more

以下是代码:

import static com.ca.svcode.protocols.http.fluent.HttpFluentInterface.*;

import static org.junit.Assert.*;

import static com.ca.svcode.protocols.http.fluent.HttpFluentInterface.forGet;

import com.ca.svcode.engine.junit4.VirtualServerRule;

import org.junit.Rule;

import org.junit.Test;

public class TestClass {

    GetCurrentWeather gcw = new GetCurrentWeather();
    String URL = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=14c206d2c4fc6e04f152395f98fbfe20";

    String JSON_EXAMPLES_PORTFOLIO = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"stations\",\"main\":{\"temp\":291.6,\"pressure\":1018,\"humidity\":48,\"temp_min\":291.15,\"temp_max\":292.15},\"visibility\":10000,\"wind\":{\"speed\":8.2,\"deg\":270},\"clouds\":{\"all\":56},\"dt\":1503161400,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0042,\"country\":\"GB\",\"sunrise\":1503118420,\"sunset\":1503169985},\"id\":2643743,\"name\":\"London\",\"cod\":200}";
    int CUSTOM_STATUS_CODE = 200;

    @Rule
    public VirtualServerRule vs = new VirtualServerRule();

    @Test
    public void testSimpleHttpGetWithResponseCodeAndStringBody() throws Exception {

        forGet(URL).doReturn(aMessage(200).withJsonBody("{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"stations\",\"main\":{\"temp\":291.6,\"pressure\":1018,\"humidity\":48,\"temp_min\":291.15,\"temp_max\":292.15},\"visibility\":10000,\"wind\":{\"speed\":8.2,\"deg\":270},\"clouds\":{\"all\":56},\"dt\":1503161400,\"sys\":{\"type\":1,\"id\":5091,\"message\":0.0042,\"country\":\"GB\",\"sunrise\":1503118420,\"sunset\":1503169985},\"id\":2643743,\"name\":\"London\",\"cod\":200}"));
        /*GetCurrentWeather gw = new GetCurrentWeather();
        String printResponse = gw.place("http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=14c206d2c4fc6e04f152395f98fbfe20");
        System.out.println(printResponse);*/
        assertEquals(200, CUSTOM_STATUS_CODE);
      }
    }

请建议。

1 个答案:

答案 0 :(得分:0)

其中一个SV jar,依赖于JDK1.8 jar(tools.jar),因此我们需要在项目的buildpath中添加JDK1.8库。