|
| 1 | +# JUnit 5 Integration |
| 2 | +**@Since will be at next release** |
| 3 | + |
| 4 | +Moco makes use of Extension in JUnit 5 to simplify JUnit integration. |
| 5 | + |
| 6 | +@MocoExtension is required to use Moco in JUnit 5. Every test class use Moco should be annotated with @ExtendWith(MocoExtension.class). |
| 7 | + |
| 8 | +```java |
| 9 | +@ExtendWith(MocoExtension.class) |
| 10 | +class WithMocoTest { |
| 11 | + ... |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +With @MocoExtesion, Moco server will be started before test and stopped after test. |
| 16 | + |
| 17 | +## HTTP Server |
| 18 | + |
| 19 | +@MocoHttpServer could be used to start a HTTP server. Configuration file and port are required. |
| 20 | + |
| 21 | +```java |
| 22 | +@ExtendWith(MocoExtension.class) |
| 23 | +@MocoHttpServer(filepath = "foo/foo.json", port=12306) |
| 24 | +public class MocoHttpTest { |
| 25 | + @Test |
| 26 | + public void should_return_expected_message() throws IOException { |
| 27 | + Content content = Request.Get("http://localhost:12306").execute().returnContent(); |
| 28 | + assertThat(content.asString(), is("foo")); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +**filepath** is provided here, you can also use `classpath` to retrieve configuration file from classpath. |
| 34 | + |
| 35 | +```java |
| 36 | +@ExtendWith(MocoExtension.class) |
| 37 | +@MocoHttpServer(classpath = "foo.json", port=12306) |
| 38 | +public class MocoHttpTest { |
| 39 | + ... |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## HTTPS Server |
| 44 | + |
| 45 | +Besides @MocoHttpServer, you should also use @MocoCertificate to start Moco as a HTTPS server. |
| 46 | + |
| 47 | +```java |
| 48 | +@ExtendWith(MocoExtension.class) |
| 49 | +@MocoHttpServer(filepath = "foo/foo.json", port=12306) |
| 50 | +@MocoCertificate(filepath = "certificate/cert.jks", keyStorePassword = "mocohttps", certPassword = "mocohttps") |
| 51 | +public class MocoHttpTest { |
| 52 | + @Test |
| 53 | + public void should_return_expected_message() throws IOException { |
| 54 | + Content content = Request.Get("http://localhost:12306").execute().returnContent(); |
| 55 | + assertThat(content.asString(), is("foo")); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Certificate file, keyStorePassword and certPassword can be configured with @MocoCertificate. |
| 61 | + |
| 62 | +You can also configure certificate file with `classpath`. |
| 63 | + |
| 64 | +```java |
| 65 | +@ExtendWith(MocoExtension.class) |
| 66 | +@MocoHttpServer(classpath = "foo.json", port=12306) |
| 67 | +@MocoCertificate(classpath = "certificate/cert.jks", keyStorePassword = "mocohttps", certPassword = "mocohttps") |
| 68 | +public class MocoHttpTest { |
| 69 | + @Test |
| 70 | + public void should_return_expected_message() throws IOException { |
| 71 | + ... |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
0 commit comments