com.github.httpmock.rules.HttpMockServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mock-http-server-junit Show documentation
Show all versions of mock-http-server-junit Show documentation
Mock HTTP Server - JUnit Rules
package com.github.httpmock.rules;
import static com.github.httpmock.PortUtil.getRandomPorts;
import java.util.List;
import org.junit.rules.ExternalResource;
import com.github.httpmock.ServerException;
import com.github.httpmock.HttpMockServerStandalone;
public class HttpMockServer extends ExternalResource {
private HttpMockServerStandalone applicationServer;
private static final String MOCK_SERVER_CONTEXT = "/mockserver";
@Override
protected void before() throws Throwable {
startServer();
}
private void startServer() {
applicationServer = createApplicationServer();
try {
applicationServer.start();
} catch (Exception e) {
throw new ServerException(e);
}
applicationServer.deploy("target/wars/mockserver.war");
}
HttpMockServerStandalone createApplicationServer() {
List randomPorts = getRandomPorts(2);
return new HttpMockServerStandalone(randomPorts.get(0), randomPorts.get(1));
}
@Override
protected void after() {
stopServer();
}
private void stopServer() {
try {
applicationServer.stop();
} catch (Exception e) {
throw new ServerException(e);
}
}
public int getPort() {
return applicationServer.getHttpPort();
}
public MockService getMockService() {
return new MockService(getBaseUri(), MOCK_SERVER_CONTEXT);
}
private String getBaseUri() {
int port = getPort();
String host = "localhost";
return String.format("http://%s:%d", host, port);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy