Test-driven development

Report a typo

Imagine that you have a task to fix an implementation of a Person class to make it pass the following unit tests written according to business logic requirements:

@Test
fun testPersonCreationValidArgs() {
    val name = "Jane Doe"
    val age = Person.MIN_AGE + 23
    val person = Person(name, age)

    assertEquals(name, person.name)
    assertEquals(age, person.age)
}

@Test
fun testPersonCreationNegativeAge() {
    val name = "Jane Doe"
    val age = Person.MIN_AGE - 1
    val person = Person(name, age)

    assertEquals(Person.MIN_AGE, person.age)
}

@Test
fun testPersonCreationAgeOverUpperLimit() {
    val name = "Jane Doe"
    val age = Person.MAX_AGE + 1
    val person = Person(name, age)

    assertEquals(Person.MAX_AGE, person.age)
}

@Test
fun testPersonCreationNameNull() {
    val name: String? = null
    val age = Person.MIN_AGE + 1
    val person = Person(name, age)

    assertNotNull(person.name)
    assertEquals(Person.DEFAULT_NAME, person.name)
}

@Test
fun testPersonCreationNameBlank() {
    val name = "\t"
    val age = Person.MIN_AGE + 1
    val person = Person(name, age)

    assertEquals(Person.DEFAULT_NAME, person.name)
}

@Test
fun testPersonCreationNameEmpty() {
    val name = ""
    val age = Person.MIN_AGE + 1
    val person = Person(name, age)

    assertEquals(Person.DEFAULT_NAME, person.name)
}

The Person class has a constructor that accepts two arguments, a string name and an integer age, and should set the name and age fields of the object according to the criteria set out in the unit tests.

Write a program in Kotlin
class Person(personName: String?, personAge: Int) {
// Do not change this part
companion object {
const val DEFAULT_NAME = "Unknown"
const val MAX_AGE = 130
const val MIN_AGE = 0
}

// Fix this to make its code pass the unit tests
val name = personName
val age = personAge
}
___

Create a free account to access the full topic