使用powermockito模拟URL类时出现问题

时间:2015-07-29 13:16:02

标签: java unit-testing mocking powermock powermockito

如果不详细了解这样做的优点,只需要帮助找出以下测试代码无效的原因!在这一点上,这更像是一种学习练习。

只是尝试使用PowerMockito为URL类创建模拟,并为其定义一些行为。这是代码:

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);





    }

}

抛出以下异常堆栈。特别是,这个测试的linke 39,这是我所拥有的:PowerMockito.when(mockURL.openConnection())。thenReturn(mockConn);抛出错误。请注意,URL是最后一类,而我正在使用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)

3 个答案:

答案 0 :(得分:4)

这不完全是一个解决方案,我现在只是简单地毕业了一个错误,但至少令人讨厌的'AbstractMethodError'现在已经消失了。

我所做的是为prepareClassForTest注释添加以下类:

....
@PrepareForTest({URL.class, URLConnection.class, WeatherServiceImplTest.class} )
...

有点怀疑,但下面的帖子肯定了这个疑问: powermock puzzler

无论如何,祝我好运。第二天嘲笑我的方式,而且我已经全身心地准备丢球了......

答案 1 :(得分:2)

我不太确定,但尝试使用Mockito来模拟方法调用。看来我已经遇到过这样的问题,我认为PowerMockito方面存在一些错误。

我记得你是否会使用

Mockito.when(mockURL.openConnection()).thenReturn(mockConn);

而不是

PowerMockito.when(mockURL.openConnection()).thenReturn(mockConn);

它会正常工作。

或者如果错误,请尝试使用其他方式,例如

Mockito/PowerMockito.doReturn(mockConn).when(mockUrl).openConnection();

如果其中一些可行,似乎原因是PowerMockito开发团队未处理的情况。而power mockto调用真实方法以及代替模拟方法。

答案 2 :(得分:0)

URL是最终课程。要模拟最终课程,我们可以将PowerMockito与Junit结合使用。 为了模拟最终类,我们需要使用@RunWith(PowerMockRunner.class)和@PrepareForTest({URL.class})

注释测试类。

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ URL.class })
public class Test {
    @Test
    public void test() throws Exception {
        URL url = PowerMockito.mock(URL.class);
        HttpURLConnection huc = Mockito.mock(HttpURLConnection.class);
        PowerMockito.when(url.openConnection()).thenReturn(huc);
        assertTrue(url.openConnection() instanceof HttpURLConnection);
    }
}

但是在 PowerMockito.when(url.openConnection())。thenReturn(huc); 行中,抛出以下错误:

java.lang.AbstractMethodError
    at java.net.URL.openConnection(URL.java:971)
    at java_net_URL$openConnection.call(Unknown Source) 

为了消除此错误,我们可以如下所示修改Test类:

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ URL.class })
public class Test {
    @Test
    public void test() throws Exception {

        public class UrlWrapper {

            URL url;

            public UrlWrapper(String spec) throws MalformedURLException {
                url = new URL(spec);
            }

            public URLConnection openConnection() throws IOException {
                return url.openConnection();
            }
        }

        UrlWrapper url = Mockito.mock(UrlWrapper.class);
        HttpURLConnection huc = Mockito.mock(HttpURLConnection.class);
        PowerMockito.when(url.openConnection()).thenReturn(huc);
        assertTrue(url.openConnection() instanceof HttpURLConnection);
    }
}

访问:https://programmingproblemsandsolutions.blogspot.com/2019/04/abstractmethoderror-is-thrown-on.html