6

无需详细说明这样做的优点,只需要帮助找出以下测试代码不起作用的原因!在这一点上,这更像是一种学习练习。

只是尝试使用 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)
4

3 回答 3

5

好吧,这并不完全是一个解决方案,我现在只是简单地毕业了一个不同的错误,但至少恼人的“AbstractMethodError”现在已经消失了。

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

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

有点可疑,但下面的帖子肯定了这个怀疑: powermockuzzler

不管怎样,祝我好运。第二天嘲笑我的方式,我都搞砸了,准备丢球......

于 2015-07-29T13:48:36.247 回答
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 调用真实方法以及或代替模拟方法。

于 2015-07-29T13:36:13.890 回答
1

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) 

为了摆脱这个错误,我们可以修改我们的测试类,如下所示:

@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-throw-on.html

于 2019-04-01T10:02:20.393 回答