Skip to content

Commit c152fd8

Browse files
authored
Use wiremock instead of okhttp-mockwebserver. (#691)
Use wiremock instead of okhttp-mockwebserver. - okhttp-mockwebserver depends on JUnit4. - okhttp-mockwebserver alpha version can use with JUnit5. But it's not released yet.
1 parent 834592d commit c152fd8

File tree

14 files changed

+339
-200
lines changed

14 files changed

+339
-200
lines changed

build.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import com.github.spotbugs.snom.SpotBugsTask
18+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
1819
import org.springframework.boot.gradle.plugin.SpringBootPlugin
1920

2021
// ./gradlew clean && ./gradlew uploadArchives -Prelease
@@ -93,6 +94,7 @@ subprojects {
9394
dependencies {
9495
dependency 'com.google.guava:guava:' + ext['guava.version']
9596
dependency 'com.github.stefanbirkner:system-lambda:1.2.0'
97+
dependency "com.github.tomakehurst:wiremock-jre8:2.30.1"
9698
dependencySet(group: 'com.squareup.retrofit2', version: ext['retrofit.version']) {
9799
entry 'converter-jackson'
98100
entry 'retrofit'
@@ -111,7 +113,7 @@ subprojects {
111113

112114
testImplementation 'com.google.guava:guava'
113115
testImplementation 'com.github.stefanbirkner:system-lambda'
114-
testImplementation 'com.squareup.okhttp3:mockwebserver'
116+
testImplementation 'com.github.tomakehurst:wiremock-jre8'
115117
testImplementation 'org.hibernate.validator:hibernate-validator'
116118
testImplementation 'org.springframework.boot:spring-boot-starter-test' // MockHttpServletRequest
117119
testImplementation 'org.springframework.boot:spring-boot-starter-logging'
@@ -221,6 +223,14 @@ subprojects {
221223

222224
test {
223225
useJUnitPlatform()
226+
testLogging {
227+
// Make sure output from standard out or error is shown in Gradle output.
228+
showStandardStreams true
229+
showExceptions true
230+
showCauses true
231+
showStackTraces true
232+
exceptionFormat TestExceptionFormat.FULL
233+
}
224234
}
225235

226236
project.plugins.withType(SpringBootPlugin) {

line-bot-api-client/src/test/java/com/linecorp/bot/client/AbstractWiremockTest.java

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,64 @@
1616

1717
package com.linecorp.bot.client;
1818

19+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
20+
1921
import java.net.URI;
2022

2123
import org.junit.After;
2224
import org.junit.Before;
2325
import org.slf4j.bridge.SLF4JBridgeHandler;
2426

25-
import com.fasterxml.jackson.databind.ObjectMapper;
26-
import com.fasterxml.jackson.databind.ObjectWriter;
27-
28-
import com.linecorp.bot.model.error.ErrorResponse;
29-
30-
import lombok.SneakyThrows;
31-
import okhttp3.mockwebserver.MockResponse;
32-
import okhttp3.mockwebserver.MockWebServer;
27+
import com.github.tomakehurst.wiremock.WireMockServer;
28+
import com.github.tomakehurst.wiremock.client.WireMock;
3329

3430
public abstract class AbstractWiremockTest {
3531
public static final int ASYNC_TEST_TIMEOUT = 1_000;
36-
private static final ObjectWriter ERROR_RESPONSE_READER = new ObjectMapper().writerFor(ErrorResponse.class);
3732

3833
static {
3934
SLF4JBridgeHandler.install();
4035
SLF4JBridgeHandler.removeHandlersForRootLogger();
4136
}
4237

43-
protected MockWebServer mockWebServer;
38+
protected WireMockServer wireMockServer;
4439
protected LineMessagingClient lineMessagingClient;
4540
protected LineBlobClient lineBlobClient;
4641
protected ChannelManagementSyncClient channelManagementSyncClient;
4742

4843
@Before
4944
public void setUpWireMock() {
50-
mockWebServer = new MockWebServer();
51-
lineMessagingClient = createLineMessagingClient(mockWebServer);
52-
lineBlobClient = createLineBlobClient(mockWebServer);
53-
channelManagementSyncClient = createChannelManagementSyncClient(mockWebServer);
54-
}
45+
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
46+
wireMockServer.start();
47+
WireMock.configureFor("localhost", wireMockServer.port());
5548

56-
@After
57-
public void shutDownWireMock() throws Exception {
58-
mockWebServer.shutdown();
49+
lineMessagingClient = createLineMessagingClient(wireMockServer);
50+
lineBlobClient = createLineBlobClient(wireMockServer);
51+
channelManagementSyncClient = createChannelManagementSyncClient(wireMockServer);
5952
}
6053

61-
@SneakyThrows
62-
public void mocking(final int responseCode, final ErrorResponse errorResponse) {
63-
mockWebServer
64-
.enqueue(new MockResponse()
65-
.setResponseCode(responseCode)
66-
.setBody(ERROR_RESPONSE_READER.writeValueAsString(errorResponse)));
54+
@After
55+
public void shutDownWireMock() {
56+
wireMockServer.stop();
6757
}
6858

69-
protected LineMessagingClient createLineMessagingClient(final MockWebServer mockWebServer) {
59+
protected LineMessagingClient createLineMessagingClient(final WireMockServer wireMockServer) {
7060
return LineMessagingClient.builder("token")
71-
.apiEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
72-
.blobEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
61+
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
62+
.blobEndPoint(URI.create(wireMockServer.baseUrl()))
7363
.build();
7464
}
7565

76-
protected LineBlobClient createLineBlobClient(MockWebServer mockWebServer) {
66+
protected LineBlobClient createLineBlobClient(WireMockServer wireMockServer) {
7767
return LineBlobClient.builder("token")
78-
.apiEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
68+
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
7969
.build();
8070
}
8171

82-
protected ChannelManagementSyncClient createChannelManagementSyncClient(final MockWebServer mockWebServer) {
72+
protected ChannelManagementSyncClient createChannelManagementSyncClient(
73+
final WireMockServer wireMockServer) {
8374
return ChannelManagementSyncClient
8475
.builder(() -> "token")
85-
.apiEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
76+
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
8677
.build();
8778
}
8879
}

line-bot-api-client/src/test/java/com/linecorp/bot/client/ChannelManagementSyncClientIntegrationWiremockTest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717
package com.linecorp.bot.client;
1818

19+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
24+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
25+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
1926
import static org.assertj.core.api.Assertions.assertThat;
2027

2128
import java.net.URI;
22-
import java.nio.charset.StandardCharsets;
2329

2430
import org.junit.Test;
2531

@@ -31,34 +37,31 @@
3137
import com.linecorp.bot.liff.response.LiffAppAddResponse;
3238
import com.linecorp.bot.model.objectmapper.ModelObjectMapper;
3339

34-
import okhttp3.mockwebserver.MockResponse;
35-
import okhttp3.mockwebserver.RecordedRequest;
36-
3740
public class ChannelManagementSyncClientIntegrationWiremockTest
3841
extends AbstractWiremockTest {
3942
private static final ObjectMapper OBJECT_MAPPER = ModelObjectMapper.createNewObjectMapper();
4043

41-
@Test(timeout = ASYNC_TEST_TIMEOUT)
44+
@Test(timeout = 10_000)
4245
public void testAddLiffMenu() throws Exception {
4346
// Mocking
4447
LiffAppAddResponse response = new LiffAppAddResponse("NEW_LIFF_ID");
45-
mockWebServer.enqueue(new MockResponse().setResponseCode(200)
46-
.setBody(OBJECT_MAPPER.writeValueAsString(response)));
48+
stubFor(
49+
post("/liff/v1/apps")
50+
.willReturn(
51+
aResponse()
52+
.withStatus(200)
53+
.withBody(OBJECT_MAPPER.writeValueAsString(response))
54+
)
55+
);
4756

4857
// Do
4958
LiffView liffView = new LiffView(Type.COMPACT, URI.create("https://example.com"));
5059
LiffAppAddRequest request = new LiffAppAddRequest(liffView);
5160
final LiffAppAddResponse liffAppAddResponse = channelManagementSyncClient.addLiffApp(request);
5261

5362
// Verify
54-
final RecordedRequest recordedRequest = mockWebServer.takeRequest();
55-
final LiffAppAddRequest requestedBody = OBJECT_MAPPER
56-
.readValue(recordedRequest.getBody().readString(StandardCharsets.UTF_8),
57-
LiffAppAddRequest.class);
58-
assertThat(requestedBody)
59-
.isEqualTo(request);
60-
assertThat(recordedRequest.getPath())
61-
.isEqualTo("/liff/v1/apps");
63+
verify(postRequestedFor(urlEqualTo("/liff/v1/apps"))
64+
.withRequestBody(equalTo(OBJECT_MAPPER.writeValueAsString(request))));
6265
assertThat(liffAppAddResponse.getLiffId())
6366
.isEqualTo("NEW_LIFF_ID");
6467
}

line-bot-api-client/src/test/java/com/linecorp/bot/client/HeaderInterceptorWireMockTest.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package com.linecorp.bot.client;
1818

19-
import static java.util.Collections.singletonList;
20-
import static org.assertj.core.api.Assertions.assertThat;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
24+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
25+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
2126
import static org.mockito.Mockito.when;
2227

2328
import java.net.URI;
@@ -28,8 +33,7 @@
2833
import org.mockito.junit.MockitoJUnit;
2934
import org.mockito.junit.MockitoRule;
3035

31-
import okhttp3.mockwebserver.MockWebServer;
32-
import okhttp3.mockwebserver.RecordedRequest;
36+
import com.github.tomakehurst.wiremock.WireMockServer;
3337

3438
public class HeaderInterceptorWireMockTest extends AbstractWiremockTest {
3539
@Rule
@@ -40,29 +44,31 @@ public class HeaderInterceptorWireMockTest extends AbstractWiremockTest {
4044

4145
@Test(timeout = ASYNC_TEST_TIMEOUT)
4246
public void forChannelTokenSupplier() throws Exception {
47+
stubFor(get(urlEqualTo("/v2/bot/profile/TEST"))
48+
.willReturn(aResponse().withStatus(200)
49+
.withBody("{}")));
50+
4351
// Do
4452
when(channelTokenSupplier.get()).thenReturn("1st");
45-
lineMessagingClient.getProfile("TEST");
53+
lineMessagingClient.getProfile("TEST").get();
4654

4755
// Verify
48-
final RecordedRequest request1st = mockWebServer.takeRequest();
49-
assertThat(request1st.getHeaders().toMultimap())
50-
.containsEntry("Authorization", singletonList("Bearer 1st"));
56+
verify(getRequestedFor(urlEqualTo("/v2/bot/profile/TEST"))
57+
.withHeader("Authorization", equalTo("Bearer 1st")));
5158

5259
// Do again with another channel token.
5360
when(channelTokenSupplier.get()).thenReturn("2nd");
54-
lineMessagingClient.getProfile("TEST");
61+
lineMessagingClient.getProfile("TEST").get();
5562

5663
// Verify
57-
final RecordedRequest request2nd = mockWebServer.takeRequest();
58-
assertThat(request2nd.getHeaders().toMultimap())
59-
.containsEntry("Authorization", singletonList("Bearer 2nd"));
64+
verify(getRequestedFor(urlEqualTo("/v2/bot/profile/TEST"))
65+
.withHeader("Authorization", equalTo("Bearer 2nd")));
6066
}
6167

6268
@Override
63-
protected LineMessagingClient createLineMessagingClient(final MockWebServer mockWebServer) {
69+
protected LineMessagingClient createLineMessagingClient(final WireMockServer wireMockServer) {
6470
return LineMessagingClient.builder(channelTokenSupplier)
65-
.apiEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
71+
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
6672
.build();
6773
}
6874
}

line-bot-api-client/src/test/java/com/linecorp/bot/client/LineMessagingClientBuilderTest.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,61 @@
1616

1717
package com.linecorp.bot.client;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
24+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
25+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
2026
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2127

2228
import java.net.URI;
29+
import java.util.concurrent.ExecutionException;
2330

2431
import org.junit.Rule;
2532
import org.junit.Test;
2633
import org.mockito.junit.MockitoJUnit;
2734
import org.mockito.junit.MockitoRule;
2835

29-
import okhttp3.mockwebserver.RecordedRequest;
30-
3136
public class LineMessagingClientBuilderTest extends AbstractWiremockTest {
3237
@Rule
3338
public final MockitoRule mockitoRule = MockitoJUnit.rule();
3439

3540
@Test
36-
public void testBuildWithFixedToken() throws InterruptedException {
41+
public void testBuildWithFixedToken() throws InterruptedException, ExecutionException {
42+
stubFor(get(urlEqualTo("/v2/bot/profile/TEST"))
43+
.willReturn(aResponse().withBody("{}")));
44+
3745
lineMessagingClient = new LineMessagingClientBuilder()
3846
.channelToken("MOCKED_TOKEN")
39-
.apiEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
47+
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
4048
.build();
4149

4250
// Do
43-
lineMessagingClient.getProfile("TEST");
51+
lineMessagingClient.getProfile("TEST").get();
4452

4553
// Verify
46-
final RecordedRequest recordedRequest = mockWebServer.takeRequest();
47-
assertThat(recordedRequest.getHeader("Authorization"))
48-
.isEqualTo("Bearer MOCKED_TOKEN");
54+
verify(getRequestedFor(urlEqualTo("/v2/bot/profile/TEST"))
55+
.withHeader("Authorization", equalTo("Bearer MOCKED_TOKEN")));
4956
}
5057

5158
@Test
52-
public void testBuilderWithChannelTokenSupplier() throws InterruptedException {
59+
public void testBuilderWithChannelTokenSupplier() throws InterruptedException, ExecutionException {
60+
stubFor(get(urlEqualTo("/v2/bot/profile/TEST"))
61+
.willReturn(aResponse().withBody("{}")));
62+
5363
lineMessagingClient =
5464
LineMessagingClient.builder(() -> "MOCKED_TOKEN")
55-
.apiEndPoint(URI.create("http://localhost:" + mockWebServer.getPort()))
65+
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
5666
.build();
5767

5868
// Do
59-
lineMessagingClient.getProfile("TEST");
69+
lineMessagingClient.getProfile("TEST").get();
6070

6171
// Verify
62-
final RecordedRequest recordedRequest = mockWebServer.takeRequest();
63-
assertThat(recordedRequest.getHeader("Authorization"))
64-
.isEqualTo("Bearer MOCKED_TOKEN");
72+
verify(getRequestedFor(urlEqualTo("/v2/bot/profile/TEST"))
73+
.withHeader("Authorization", equalTo("Bearer MOCKED_TOKEN")));
6574
}
6675

6776
@Test

line-bot-api-client/src/test/java/com/linecorp/bot/client/LineMessagingClientImplRichMenuWiremockTest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@
1616

1717
package com.linecorp.bot.client;
1818

19+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
1923
import static java.util.Collections.emptyList;
2024
import static org.assertj.core.api.Assertions.assertThat;
2125

2226
import org.junit.Test;
2327

2428
import com.linecorp.bot.model.response.BotApiResponse;
2529

26-
import okhttp3.mockwebserver.MockResponse;
27-
2830
public class LineMessagingClientImplRichMenuWiremockTest extends AbstractWiremockTest {
2931
public static final BotApiResponseBody SUCCESS_BODY = new BotApiResponseBody("", emptyList());
3032
public static final BotApiResponse SUCCESS = SUCCESS_BODY.withRequestId("REQUEST_ID");
3133

3234
@Test(timeout = ASYNC_TEST_TIMEOUT)
3335
public void status200WithoutBodyTest() throws Exception {
3436
// Mocking
35-
mockWebServer.enqueue(new MockResponse().setResponseCode(200)
36-
.addHeader("x-line-request-id", "REQUEST_ID"));
37+
stubFor(delete(urlEqualTo("/v2/bot/richmenu/RICH_MENU_ID")).willReturn(
38+
aResponse()
39+
.withStatus(200)
40+
.withHeader("x-line-request-id", "REQUEST_ID")
41+
));
3742

3843
// Do
3944
final BotApiResponse botApiResponse = lineMessagingClient.deleteRichMenu("RICH_MENU_ID").get();
@@ -43,12 +48,16 @@ public void status200WithoutBodyTest() throws Exception {
4348
@Test(timeout = ASYNC_TEST_TIMEOUT)
4449
public void status200WithBodyTest() throws Exception {
4550
// Mocking
46-
mockWebServer.enqueue(new MockResponse().setResponseCode(200)
47-
.addHeader("x-line-request-id", "REQUEST_ID")
48-
.setBody("{}"));
51+
stubFor(delete(urlEqualTo("/v2/bot/richmenu/RICH_MENU_ID")).willReturn(
52+
aResponse()
53+
.withStatus(200)
54+
.withHeader("x-line-request-id", "REQUEST_ID")
55+
.withBody("{}")
56+
));
4957

5058
// Do
51-
final BotApiResponse botApiResponse = lineMessagingClient.deleteRichMenu("RICH_MENU_ID").get();
59+
final BotApiResponse botApiResponse = lineMessagingClient.deleteRichMenu("RICH_MENU_ID")
60+
.get();
5261
assertThat(botApiResponse).isEqualTo(SUCCESS);
5362
}
5463
}

0 commit comments

Comments
 (0)