You have several ways to use Moco. One is API, which you can use in your unit test. The other is that run Moco as standalone. Currently, you put all your configuration in JSON file.
On the other hand, Moco has several different ways to integrate with some tools: Maven plugin, Gradle plugin and shell support.
- API
- Standalone
- JSON configuration in Java API
- HTTPS
- Socket
- JUnit Integration
- Maven Plugin
- Gradle Plugin
- Shell
- Scala
Moco has been published on Maven repository, so you can refer to it directly in your dependency. This is core dependency
<dependency>
<groupId>com.github.dreamhead</groupId>
<artifactId>moco-core</artifactId>
<version>1.5.0</version>
</dependency>
A gradle example is as follow:
repositories {
mavenCentral()
}
dependencies {
testImplementation(
"com.github.dreamhead:moco-core:1.5.0",
)
}
Here is an typical Moco test case in JUnit.
import org.junit.jupiter.api.Test;
import java.io.IOException;
import com.github.dreamhead.moco.HttpServer;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import static com.github.dreamhead.moco.Moco.*;
import static com.github.dreamhead.moco.Runner.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;
@Test
public void should_response_as_expected() throws Exception {
HttpServer server = httpServer(12306);
server.response("foo");
running(server, () -> {
Content content = Request.Get("http://localhost:12306").execute().returnContent();
assertThat(content.asString(), is("foo"));
});
}
As shown above, we created a new server and configure it as expected. And then run our test against this server.
Here, We use Apache Http Client Fluent API to request our testing server.
You may need to control server start/stop by yourself, for example, in your Before (or setup) method, runner API could be used.
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static com.github.dreamhead.moco.Moco.httpServer;
import static com.github.dreamhead.moco.Runner.runner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class MocoRunnerTest {
private Runner runner;
@BeforeEach
public void setup() {
HttpServer server = httpServer(12306);
server.response("foo");
runner = runner(server);
runner.start();
}
@AfterEach
public void tearDown() {
runner.stop();
}
@Test
public void should_response_as_expected() throws IOException {
Content content = Request.Get("http://localhost:12306").execute().returnContent();
assertThat(content.asString(), is("foo"));
}
}
Moco can be used as standalone to run with configuration and you can download standalone directly: Standalone Moco Runner
First of all, a JSON configuration file is required to start Moco.
[
{
"response" :
{
"text" : "foo"
}
}
]
(foo.json)
It's time to run Moco standalone server:
java -jar moco-runner-<version>-standalone.jar http -p 12306 -c foo.json
Now, open your browser and input "http://localhost:12306". You will see "foo". That's it.
@Since 0.10.0
If you have setup your server with JSON configuration, you can also run with your configuration from Java API.
import static com.github.dreamhead.moco.Moco.file;
import static com.github.dreamhead.moco.Moco.pathResource;
import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer;
import static com.github.dreamhead.moco.Runner.running;
import static com.github.dreamhead.moco.helper.RemoteTestUtils.port;
import static com.github.dreamhead.moco.helper.RemoteTestUtils.root;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class MocoJsonHttpRunnerTest extends AbstractMocoStandaloneTest {
@Test
public void should_return_expected_response() throws Exception {
final HttpServer server = jsonHttpServer(12306, file("foo.json"));
running(server, () -> {
Content content = Request.Get("http://localhost:12306").execute().returnContent();
assertThat(content.asString(), is("foo"));
});
}
}
HTTPS is also a mainstream usage for HTTP protocol. Moco supports HTTPS as well. The main difference in API is that a certificate is required for HTTPS. On the other hand, httpsServer should be used.
final HttpsCertificate certificate = certificate(pathResource("cert.jks"), "mocohttps", "mocohttps");
final HttpsServer server = httpsServer(certificate, hit);
If you want to use HTTPS for standalone server. certificate information could be provided as CLI arguments.
java -jar moco-runner-<version>-standalone.jar https -p 12306 -c foo.json --https /path/to/cert.jks --cert mocohttps --keystore mocohttps
Socket is a common integration channel. There is only content available in socket.
final SocketServer server = socketServer(12306);
You can also use socket in standalone server.
java -jar moco-runner-<version>-standalone.jar socket -p 12306 -c foo.json
More socket APIs can be found here.
@Since 0.11.0
Moco provides JUnit integration to simplify the use of Moco in JUnit.
More details can be found in here.
Moco also can be used as Maven plugin.
https://github.com/GarrettHeel/moco-maven-plugin
Moco can be used in Gradle
https://github.com/silverjava/moco-gradle-plugin
If you are using Mac or Linux, you may try the following approach:
- Make sure you have JDK 6 or later.
- Download the script.
- Place it on your $PATH. (~/bin is a good choice if it is on your path.)
- Set it to be executable. (chmod 755 ~/bin/moco)
Now, you can try
moco http -p 12306 -c foo.json
It will download the latest moco automatically if you don't have locally.
Scala fans can find Scala wrapper in