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.