mercredi 29 juillet 2015

issue while using powermockito to mock the URL class

Without going into the details of the merit of doing this way, just need help figuring out why the following test codes do not work! This has been more of a learning exercise at this point..

Just trying to use PowerMockito to create a mock for the URL class, and define some behaviors for it. Here's the code:

package com.icidigital.services

import com.icidigital.services.impl.WeatherServiceImpl
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner


/**
 * Created by apil.tamang on 7/27/15.
 * I could not get the setup to finish! Failed!
 */
@PrepareForTest(URL.class)
@RunWith(PowerMockRunner.class)
class WeatherServiceImplTest {


    URL mockURL;
    URLConnection mockConn;

    @Before
    public void setUp(){

        byte[] data = "123,456".getBytes();

        InputStream input = new ByteArrayInputStream(data);

        //define and set behavior for mockConn
        mockConn=PowerMockito.mock(HttpURLConnection.class);
        //Mockito.doCallRealMethod().when(mockConn).getResponseCode();
        //Mockito.when(mockConn.getResponseCode()).thenCallRealMethod().thenReturn(200);
        //Mockito.when(mockConn.getInputStream()).thenReturn(input);

        //define and set behavior for mockURLObj
        mockURL=PowerMockito.mock(URL.class);
        PowerMockito.when(mockURL.openConnection()).thenReturn(mockConn);


    }

    @Test
    public void testSetup(){

        WeatherServiceImpl testObj=new WeatherServiceImpl(mockURL);
        String response=testObj.run("foobar");
        PowerMockito.verifyNew(mockURL);





    }

}

The following exception stack is thrown. In particular, linke 39 of this test, which is where I have: PowerMockito.when(mockURL.openConnection()).thenReturn(mockConn); throws the error. Mind you that URL is a final class, and that's I'm using Powermockito.

java.lang.AbstractMethodError
    at java.net.URL.openConnection(URL.java:971)
    at java_net_URL$openConnection.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at com.icidigital.services.WeatherServiceImplTest.setUp(WeatherServiceImplTest.groovy:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:93)

Aucun commentaire:

Enregistrer un commentaire