mirror of
https://github.com/BenCodez/VotingPlugin.git
synced 2026-11-07 10:12:36 -05:00
29 lines
1.0 KiB
Java
29 lines
1.0 KiB
Java
package com.bencodez.votingplugin.tests;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.InputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
public class HttpURLConnectionMock {
|
|
|
|
public static void setupMockHttpURLConnection(String playerName, int responseCode, String responseBody)
|
|
throws Exception {
|
|
// Mock URL and HttpURLConnection
|
|
URL mockUrl = mock(URL.class);
|
|
HttpURLConnection mockConnection = mock(HttpURLConnection.class);
|
|
|
|
when(mockUrl.openConnection()).thenReturn(mockConnection);
|
|
when(mockConnection.getResponseCode()).thenReturn(responseCode);
|
|
InputStream mockInputStream = new ByteArrayInputStream(responseBody.getBytes());
|
|
when(mockConnection.getInputStream()).thenReturn(mockInputStream);
|
|
|
|
// When new URL is called, return the mocked URL
|
|
// Note: This requires PowerMockito or a similar tool to mock the `new` keyword
|
|
// Here, it's simplified and assumes the method uses a helper that can be mocked
|
|
}
|
|
}
|