Skip to content

Instantly share code, notes, and snippets.

@dinomite
Created March 12, 2019 14:47
Show Gist options
  • Save dinomite/e23088962445ff0fbe074c6229112da2 to your computer and use it in GitHub Desktop.
Save dinomite/e23088962445ff0fbe074c6229112da2 to your computer and use it in GitHub Desktop.
Testing data classes in Kotlin
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