以下是必須針對真偽場景測驗 Socket 連接的代碼片段。
public boolean pingHost(String hostname, int port) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(hostname, port), 3000);
return true;
} catch (IOException e) {
return false;
}
}
uj5u.com熱心網友回復:
這是模擬的標準用例。使用 PowerMock ie,您可以模擬建構式呼叫并回傳模擬物件。您也可以在建構式呼叫中驗證正確的引數。只需搜索“Powermock 模擬建構式”。如果模擬不是您的選擇,另一種選擇是使用可用于 *NIX 和 Windows 平臺的 netcat 偵聽埠。
編輯 2021.12.16 -----------------
您需要包含這四個 jar 才能使用 Mockito 和 PowerMock (Maven pom)
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-inline -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<!-- https://www.baeldung.com/intro-to-powermock -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
但是有很多關于如何設定 Mockito 和 PowerMock 的檔案。
這是一個帶有一些注釋的可運行示例,讓您大致了解如何使用模擬進行測驗。
package __scratchpad__;
import static org.junit.Assert.fail;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(PingerTest.Pinger.class)
public class PingerTest {
// Inner class just to have a compact 'all in one' example
public class Pinger {
// refactored to better coding style (avoid 'Magic numbers')
// AND for testability because TIM-OUT is now accessible for test
public static final int TIME_OUT = 3000;
public boolean pingHost(String hostname, int port) {
try (Socket socket = new Socket()){
socket.connect(new InetSocketAddress(hostname, port), TIME_OUT);
return true;
} catch (IOException e) {
return false;
}
}
}
@Test
public void testPingHost() {
// create required mock objects to inject
InetSocketAddress addressMock = Mockito.mock(InetSocketAddress.class);
Socket socketMock = Mockito.mock(Socket.class);
// create parameters
final String hostname = "answer.deepthought";
final int port = 42;
final int timeOut = PingerTest.Pinger.TIME_OUT;
// create class under test
Pinger pingerUnderTest = new Pinger();
try {
// mock constructor call of class InetSocketAddress and verify correct parameters
// If parameters of call are different to the expected, than NO mock object is returned, the test will fail
// Otherwise the addressMock object is returned
whenNew(InetSocketAddress.class)
.withArguments(hostname, port)
.thenReturn(addressMock);
// mock constructor call of Socket class
whenNew(Socket.class)
.withNoArguments()
.thenReturn(socketMock);
// call method under test with parameters defined earlier.
// If parameters are different to them of InetSocketAddress constructor call the test will fail.
pingerUnderTest.pingHost(hostname, port);
// if all works fine exactly one call to method connect() with specified parameters MUST appear
// if there are more or less calls or any of the parameters is different, than the test will fail
Mockito.verify(socketMock).connect(addressMock, timeOut);
} catch(Exception e) {
e.printStackTrace();
fail("Unexpected exception caught!");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/390035.html
