[英]Not able to mock java.net.URL even by using PowerMock?
根据我的要求,我必须使用预先签名的URL将文件上传到Amazon S3,该URL是我从适用于我的AWS网站获取的代码,但现在我必须为使用PowerMockito的代码编写JUnit。模拟java.net。 URL,但根本不起作用。 以下是我的Java代码以及无法正常工作的JUnit。 谁能给我解决方案,该如何进行? 提前致谢。
我的代码:-
JUnit的
@InjectMocks
App app;
@Mock
URL url;
@Mock
OutputStream outStream;
@Test
public void uploadToS3_should_return_true() throws Exception{
// no need to create instance of App, it should be initialyzed fine with @InjectMocks annotation
// define mocked behavior
HttpURLConnection connection = PowerMockito.mock(HttpURLConnection.class);
// mock connection and out stream methods
PowerMockito.when(url.openConnection()).thenReturn(connection);
PowerMockito.whenNew(URL.class).withArguments(Matchers.anyString()).thenReturn(url);
PowerMockito.when(connection.getOutputStream()).thenReturn(outStream);
PowerMockito.when(connection.getResponseCode()).thenReturn(200);
// call action
app.uploadToS3("http://something.com", "{}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.