Powermockito not working



//ClassUnderTest

import java.util.List;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PlanRestClientXX {
    private final String mySoftVimUrl;
    private final Logger logger = LoggerFactory.getLogger(PlanRestClient.class);
    public PlanRestClientXX(final String softVimUrl) {
        mySoftVimUrl = softVimUrl;
    }
    public List<String> invokeGETRequest(final String ressourceURI) {
        try {
            final ClientRequest request = createRequest(ressourceURI);
            request.accept("application/json");
            final ClientResponse response = request.get(List.class);
            final List entity = (List) response.getEntity();
            if (entity == null) {
                return null;
            }
            return entity;
        } catch (final Exception e) {
            throw new RestClientException("Exception when qyerying softvim ", e);
        }
    }
    private ClientRequest createRequest(final String ressourceURI) {
        return new ClientRequest(ressourceURI);
    }
}

//JUNIT

import java.util.ArrayList;
import java.util.List;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.nsn.ison.service.softvim.client.rest.util.URLUtil;
@RunWith(PowerMockRunner.class)
@PrepareForTest({PlanRestClient.class, URLUtil.class, ClientRequest.class})
// added classes here which are to be mocked. Included class-under-test(PlanRestClient.class) also (not sure why?)
public class PlanRestClientXXTest {
    private final String mySoftVimUrl = "https://xxx";
    PlanRestClientXX planRestClient;
    @Mock
    ClientRequest request;
    @Mock
    ClientResponse response;
    @Before
    public void setUp() throws Exception {
        planRestClient = new PlanRestClientXX(mySoftVimUrl);
    }
    @Test
    public void testInvokeGETRequest() throws Exception {
        final List<String> entity = new ArrayList<String>();
        // to mock-> new ClientRequest(ressourceURI)
        PowerMockito.whenNew(ClientRequest.class).withArguments(Mockito.anyString()).thenReturn(request);
        // to mock-> request.accept("application/json");
        Mockito.doAnswer(new Answer<Void>() {
            @Override
            public Void answer(final InvocationOnMock invocation) throws Throwable {
                // TODO Auto-generated method stub
                return null;
            }
        }).when(request).accept(Mockito.anyString());
        // to mock-> final ClientResponse response = request.get(List.class);
        Mockito.when(request.get(List.class)).thenReturn(response);
        // final List entity = (List) response.getEntity();
        Mockito.when(response.getEntity()).thenReturn(entity);
        final List<String> expectedList = planRestClient.invokeGETRequest("ressourceURI");
        Assert.assertEquals(expectedList, entity);
    }
}

例外

com.abc.impl.RestClientException: Exception when qyerying softvim 
    at com.abc.impl.PlanRestClientXX.invokeGETRequest(PlanRestClientXX.java:30)
    at com.abc.impl.PlanRestClientXXTest.testInvokeGETRequest(PlanRestClientXXTest.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.http.conn.ssl.SSLInitializationException: Failure initializing default SSL context
    at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:360)
    at org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(SSLSocketFactory.java:175)
    at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:49)
    at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:306)
    at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)
    at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.execute(ApacheHttpClient4Executor.java:182)
    at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:39)
    at org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPInterceptor.execute(AcceptEncodingGZIPInterceptor.java:40)
    at org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45)
    at org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:443)
    at org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:677)
    at org.jboss.resteasy.client.ClientRequest.get(ClientRequest.java:479)
    at org.jboss.resteasy.client.ClientRequest.get(ClientRequest.java:508)
    at com.abc.impl.PlanRestClientXX.invokeGETRequest(PlanRestClientXX.java:23)
    ... 28 more
Caused by: java.security.NoSuchAlgorithmException: class configured for KeyManagerFactory: com.sun.net.ssl.internal.ssl.KeyManagerFactoryImpl$SunX509 not a KeyManagerFactory
    at sun.security.jca.GetInstance.checkSuperClass(GetInstance.java:242)
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:221)
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:147)
    at javax.net.ssl.KeyManagerFactory.getInstance(KeyManagerFactory.java:121)
    at org.apache.http.conn.ssl.SSLSocketFactory.createSSLContext(SSLSocketFactory.java:223)
    at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:358)
    ... 44 more

有人能调查一下,告诉我这个例外到底是怎么回事吗??我是PowerMockito/Mockito的新手。我以为"final ClientResponse response = request.get(List.class);"代码会被嘲笑。但似乎真正的呼吁是发出的,而不是嘲笑。请建议如何嘲笑这个电话??

测试代码工作,有一些小的更改,用PlanRestClientXX.class:替换了PlanRestClient.class

public class PlanRestClientXX {
    ...
    private final Logger logger = LoggerFactory.getLogger(PlanRestClientXX.class);    ...

@PrepareForTest({PlanRestClientXX.class, URLUtil.class, ClientRequest.class})

相关内容

  • 没有找到相关文章

最新更新