Skip to content

Commit

Permalink
Merge pull request #22 from komamitsu/junit5
Browse files Browse the repository at this point in the history
Use JUnit 5
  • Loading branch information
komamitsu authored Jan 28, 2024
2 parents f2071fa + b411e67 commit 31efccc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
7 changes: 2 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ repositories {
mavenCentral()
}

// TODO: Use JUnit5
/*
tasks.withType(Test) {
useJUnitPlatform()
}
*/

java {
withJavadocJar()
withSourcesJar()
}

dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.1'
testImplementation 'ch.qos.logback:logback-classic:1.1.8'
testImplementation 'org.hamcrest:hamcrest-all:1.3'
testImplementation 'com.squareup.retrofit2:retrofit:2.9.0'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.12.0'
implementation 'org.slf4j:slf4j-api:2.0.9'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package org.komamitsu.retrofit.converter.msgpack;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Objects;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import okio.Buffer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.*;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import retrofit2.Call;
import retrofit2.Response;
Expand All @@ -21,7 +17,8 @@
import retrofit2.http.POST;

public class MessagePackConverterFactoryTest {
@Rule public final MockWebServer server = new MockWebServer();
private static MockWebServer server;

private Service service;

public static class Pojo {
Expand Down Expand Up @@ -52,30 +49,15 @@ public String getS() {

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pojo pojo = (Pojo) o;

if (i != pojo.i) {
return false;
}
if (Float.compare(pojo.f, f) != 0) {
return false;
}
return s != null ? s.equals(pojo.s) : pojo.s == null;
return i == pojo.i && Float.compare(pojo.f, f) == 0 && Objects.equals(s, pojo.s);
}

@Override
public int hashCode() {
int result = i;
result = 31 * result + (f != +0.0f ? Float.floatToIntBits(f) : 0);
result = 31 * result + (s != null ? s.hashCode() : 0);
return result;
return Objects.hash(i, f, s);
}
}

Expand All @@ -84,7 +66,18 @@ interface Service {
Call<Pojo> postPojo(@Body Pojo pojo);
}

@Before
@BeforeAll
public static void beforeAll() throws IOException {
server = new MockWebServer();
server.start();
}

@AfterAll
public static void afterAll() throws IOException {
server.close();
}

@BeforeEach
public void setUp() {
Retrofit retrofit =
new Retrofit.Builder()
Expand All @@ -100,16 +93,17 @@ public void requestBodyConverter() throws IOException, InterruptedException {

Pojo requestPojo = new Pojo(42, (float) Math.PI, "Hello");
Pojo responsePojo = new Pojo(99, 1.23f, "World");
server.enqueue(
new MockResponse()
.setBody(new Buffer().write(objectMapper.writeValueAsBytes(responsePojo))));
try (Buffer buffer = new Buffer()) {
buffer.write(objectMapper.writeValueAsBytes(responsePojo));
server.enqueue(new MockResponse().setBody(buffer));

Response<Pojo> response = service.postPojo(requestPojo).execute();
assertThat(response.body(), is(responsePojo));
Response<Pojo> response = service.postPojo(requestPojo).execute();
Assertions.assertEquals(responsePojo, response.body());

RecordedRequest recordedRequest = server.takeRequest();
Pojo recordedPojo =
objectMapper.readValue(recordedRequest.getBody().readByteArray(), Pojo.class);
assertThat(recordedPojo, is(requestPojo));
RecordedRequest recordedRequest = server.takeRequest();
Pojo recordedPojo =
objectMapper.readValue(recordedRequest.getBody().readByteArray(), Pojo.class);
Assertions.assertEquals(requestPojo, recordedPojo);
}
}
}

0 comments on commit 31efccc

Please sign in to comment.