gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:12:54 26 4
gpt4 key购买 nike

无需深入了解这样做的优点,只需要帮助弄清楚为什么以下测试代码不起作用!在这一点上,这更像是一种学习练习。

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





}

}

抛出以下异常堆栈。特别是,这个测试的链接 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)

最佳答案

嗯,这不完全是一个解决方案,我现在只是解决了一个不同的错误,但至少烦人的“AbstractMethodError”现在已经消失了。

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

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

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

无论如何,祝我好运。 mock 我的第二天,我一团糟,准备丢球......

关于java - 使用 powermockito 模拟 URL 类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31701547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com