ptheocharis
02/27/2019, 5:36 PMGreeting
object, allong with a GreetingController
like the below:
data class GreetingDto(
val greeting: String? = null,
val name: String? = null
)
class GreetingController {
@GetMapping("/greeting")
fun greeting(): GreetingDto {
return GreetingDto("Hello", "World")
}
}
And I have unit test in Groovy, running with spock:
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