Skip to content

Instantly share code, notes, and snippets.

@dinomite
Last active March 12, 2019 14:50
Show Gist options
  • Save dinomite/3d2a8e93f6acb5f38feaca575d2ab78b to your computer and use it in GitHub Desktop.
Save dinomite/3d2a8e93f6acb5f38feaca575d2ab78b to your computer and use it in GitHub Desktop.
Testing data class serialization
import java.time.Instant
data class Location(@JsonProperty val userId: Int,
@JsonProperty val geohash: String,
@JsonProperty val createdAt: Instant,
@JsonProperty val updatedAt: Instant?)
import com.fasterxml.jackson.annotation.JsonProperty
import io.dropwizard.testing.FixtureHelpers.fixture
import net.forumforall.kuorum.helpers.Json
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import java.time.Instant
class LocationTest {
/*
* location.json:
*
* {
* "userId" : 7,
* "geohash" : "dnqnf41kf",
* "createdAt" : "2016-10-14T21:16:05Z",
* "updatedAt" : "2016-10-14T21:16:05Z"
* }
*/
val locationFixture = "fixtures/location.json"
val time = Instant.ofEpochSecond(1476479765)
val location = Location(7, "dnqnf41kf", time, time)
@Test
fun serializesToJson() {
assertThat(Json.asJson(location), `is`(equalTo(fixture(locationFixture))))
}
@Test
fun deserializeFromJson() {
assertThat(Json.fromJson(fixture(locationFixture), Location::class.java), `is`(location))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment