Ответ 1
Здесь образец теста. Обратите внимание, что утверждения, которые я сделал для демонстрационных целей, вам необходимо адаптировать к вашим потребностям.
@RunWith(PowerMockRunner.class)
@PrepareForTest({ toTest.class, URL.class, HttpURLConnection.class })
public class soTest {
/**
* test response.
*/
private static final String TEST_RESPONSE = "test\nresponse";
/**
* test data.
*/
private static final String DATA = RandomStringUtils.randomAscii(125);
/**
* test port.
*/
private static final int PORT = 8080;
/**
* test hosts.
*/
private static final String HOSTNAME = "hostname";
/**
* test protocol.
*/
private static final String PROTOCOL = "http";
/**
* test reference.
*/
private static final String REFERENCE = "REFERENCE";
/**
* URL mock.
*/
private URL url;
/**
* HttpURLConnection mock.
*/
private HttpURLConnection connection;
/**
* Our output.
*/
private ByteArrayOutputStream output;
/**
* Our input.
*/
private ByteArrayInputStream input;
/**
* Instance under tests.
*/
private toTest instance;
@Before
public void setUp() throws Exception
{
this.url = PowerMockito.mock(URL.class);
this.connection = PowerMockito.mock(HttpURLConnection.class);
this.output = new ByteArrayOutputStream();
this.input = new ByteArrayInputStream(TEST_RESPONSE.getBytes());
this.instance = new toTest(PROTOCOL, HOSTNAME, PORT);
PowerMockito.whenNew(URL.class).withArguments(PROTOCOL, HOSTNAME, PORT, REFERENCE).thenReturn(this.url);
}
@Test
public void testDoPost() throws Exception
{
PowerMockito.doReturn(this.connection).when(this.url).openConnection();
PowerMockito.doReturn(this.output).when(this.connection).getOutputStream();
PowerMockito.doReturn(this.input).when(this.connection).getInputStream();
final String response = this.instance.doPost(REFERENCE, DATA);
PowerMockito.verifyNew(URL.class);
new URL(PROTOCOL, HOSTNAME, PORT, REFERENCE);
// Mockito.verify(this.url).openConnection(); // cannot be verified (mockito limitation)
Mockito.verify(this.connection).getOutputStream();
Mockito.verify(this.connection).setRequestMethod("POST");
Mockito.verify(this.connection).setRequestProperty("Authorization", "Basic dGVzdDphc2Rm");
Mockito.verify(this.connection).setRequestProperty("Content-Type", "application/xml");
Mockito.verify(this.connection).setUseCaches(false);
Mockito.verify(this.connection).setDoInput(true);
Mockito.verify(this.connection).setDoOutput(true);
Mockito.verify(this.connection).getInputStream();
assertArrayEquals(DATA.getBytes(), this.output.toByteArray());
assertEquals(TEST_RESPONSE.replaceAll("\n", "\r") + "\r", response);
}
}
@Data
public class toTest {
private final String protocol, hostname;
private final int port;
public String doPost(String reference, String data) throws IOException
{
// your method, not modified
}
}
зависимости:
- commons-lang 2.5
- powermock-api-mockito 1.4.11
- powermock-module-junit4 1.4.11
- junit 4.10
- lombok 0.11.0 на тестируемый класс