Skip to content

Instantly share code, notes, and snippets.

@martinloesethjensen
Last active March 19, 2020 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinloesethjensen/f0110d09a8a962e2a3671e50b3513ebf to your computer and use it in GitHub Desktop.
Save martinloesethjensen/f0110d09a8a962e2a3671e50b3513ebf to your computer and use it in GitHub Desktop.
Kotlin Unit Testing with JUnit and Kluent
import org.amshove.kluent.`should be`
import org.amshove.kluent.shouldBeFalse
import org.amshove.kluent.shouldBeTrue
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class UserTest {
private val user = User(1, "Alice")
@Test
fun `should be able to increase reputation`() {
user.changeReputation(10)
user.reputation `should be` 10
}
@Test
fun `should be able to decrease reputation`() {
user.changeReputation(10)
user.changeReputation(-5)
user.reputation `should be` 5
}
@Nested
inner class `post should be able to` {
val editReputationLimit = 2000
@Test
fun `edit if reputation is greater than 2000`() {
user.changeReputation(editReputationLimit + 1)
user.canEditPost().shouldBeTrue()
}
@Test
fun `edit if reputation is equal to 2000`() {
user.changeReputation(editReputationLimit)
user.canEditPost().shouldBeFalse()
}
@Test
fun `edit if reputation is less than 2000`() {
user.changeReputation(editReputationLimit - 1)
user.canEditPost().shouldBeFalse()
}
}
}
class User(val id: Int, val name: String) {
var reputation = 0
private set
fun changeReputation(amount: Int) {
reputation += amount
}
fun canEditPost():Boolean {
return reputation > 2000
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment