Hi guys, new Kotlin developer here. I have the fol...
# getting-started
p
Hi guys, new Kotlin developer here. I have the following issue: I have simple
Greeting
object, allong with a
GreetingController
like the below:
Copy code
data class GreetingDto(
  val greeting: String? = null,
  val name: String? = null
)
Copy code
class GreetingController {

  @GetMapping("/greeting")
  fun greeting(): GreetingDto {
    return GreetingDto("Hello", "World")
  }
}
And I have unit test in Groovy, running with spock:
Copy code
class GreetingControllerSpec extends Specification {
  GreetingController greetingController

  def setup() {
    greetingController = new GreetingController()
  }

  def "test greeting"() {
    given:
      GreetingDto result
      GreetingDto expected = expected()
    when:
      result = greetingController.greeting()
    then:
      result == expected
  }

  GreetingDto expected() {
    new GreetingDto(
      greeting: "Hello",
      name: "World"
    )
  }
}
My problem is that the fields in the constructor which are defined with
val
, cannot be accessed by the Groovy constructor and I get this exception:
groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: greeting for class: app.greeting.GreetingDto