[英]Not able to mock java.net.URL even by using PowerMock?
According to my requirements, I have to upload a file to Amazon S3 by using pre-signed URL for which I got the code from the AWS website which worked for me but now I have to write JUnit for the same in which I used PowerMockito to mock java.net. 根据我的要求,我必须使用预先签名的URL将文件上传到Amazon S3,该URL是我从适用于我的AWS网站获取的代码,但现在我必须为使用PowerMockito的代码编写JUnit。模拟java.net。 URL but it's not working at all.
URL,但根本不起作用。 Following is my Java code plus the JUnit which is not working.
以下是我的Java代码以及无法正常工作的JUnit。 Can anyone give me solution on how should I proceed?
谁能给我解决方案,该如何进行? Thanks in advance.
提前致谢。
My code:- 我的代码:-
public class App { public Boolean uploadToS3(String presignedUrl, String jsonBody) { try { URL url = new URL(presignedUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(jsonBody); out.close(); int responseCode = connection.getResponseCode(); System.out.println("Service returned response code " + responseCode); return true; } catch (IOException io) { io.printStackTrace(); } return false; } }
JUnit JUnit的
import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Matchers; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest({URL.class,HttpURLConnection.class,OutputStreamWriter.class,OutputStream.class}) public class AppTest { @InjectMocks App app; @Test public void uploadToS3_should_return_true() throws Exception{ App app = new App(); //App app = PowerMockito.mock(App.class); URL url = PowerMockito.mock(URL.class); HttpURLConnection connection = PowerMockito.mock(HttpURLConnection.class); OutputStreamWriter outStreamWriter = PowerMockito.mock(OutputStreamWriter.class); OutputStream outStream = PowerMockito.mock(OutputStream.class); PowerMockito.whenNew(URL.class).withArguments(Matchers.anyString()).thenReturn(url); PowerMockito.when(url.openConnection()).thenReturn(connection); PowerMockito.whenNew(OutputStreamWriter.class).withArguments(Matchers.any()).thenReturn(outStreamWriter); PowerMockito.when(connection.getOutputStream()).thenReturn(outStream); PowerMockito.when(connection.getResponseCode()).thenReturn(201); app.uploadToS3("http://something.com", "{}"); } }
@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.