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

问题描述 投票:6回答:3

没有详细说明这样做的优点,只需要帮助弄清楚为什么以下测试代码不起作用!在这一点上,这更像是一次学习练习。

只是尝试使用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)
java unit-testing mocking powermock powermockito
3个回答
4
投票

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

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

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

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

无论如何,祝我好运。第二天嘲笑我的方式,我全都瘫痪,准备放球......


2
投票

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

我记得你会用的

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

代替

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

它会正常工作。

或者如果错误,请尝试使用替代方法,例如

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

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


0
投票

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


@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

© www.soinside.com 2019 - 2024. All rights reserved.