我正在尝试编写一个简单的Spring Boot控制器来渲染GORM实例并失败.
这是我的代码的缩短版本:
@RestController
@RequestMapping("/user")
class UserController {
@RequestMapping(value='/test', method=GET)
User test() {
return new User(username: 'my test username')
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Could not write JSON: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: users.domain.User["errors"]->grails.validation.ValidationErrors["messageCodesResolver"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.validation.DefaultMessageCodesResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: users.domain.User["errors"]->grails.validation.ValidationErrors["messageCodesResolver"])
该错误似乎是由GORM注入的额外属性引起的.建议的解决方案是什么?这最终会解决gorm-hibernate4-spring-boot
吗?我应该简单地禁用SerializationFeature.FAIL_ON_EMPTY_BEANS …
我想测试使用grails电子邮件插件发送电子邮件的Grails控制器.我不知道如何模仿sendMail
闭包以便交互工作.这是我最新版本的测试代码:
def 'controller should send a multipart email'() {
given: 'a mocked mailService'
controller.mailService = Mock(grails.plugin.mail.MailService)
controller.mailService.sendMail(*_) >> Mock(org.springframework.mail.MailMessage)
when:
controller.sendNow()
then:
1* _.multipart(true)
}
Run Code Online (Sandbox Code Playgroud)
控制器代码看起来像您期望的那样,例如:
def mailService
def sendNow() {
mailService.sendMail {
multipart true
to 'example@example.org'
from 'me@here.com'
subject 'a subject'
body 'a body'
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行这个测试,我得到0次调用我的multipart
交互而不是1. given:
块的第二行似乎对我很可疑,但如果我试图模拟一个Closure
而不是org.springframework.mail.MailMessage
我的测试崩溃.我还应该提到控制器本身按预期工作(它不能等我先弄清单元测试).
啊啊,几个小时之后用新鲜的头脑看着代码,我可以看出为什么上面的代码不起作用; 为了让我捕获multipart
和其他DSL调用,我将不得不模拟闭包本身,而不是sendMail方法(我不能这样做,因为闭包是在控制器本身内定义的).我大概可以做的是检查参数的sendMail
方法来查看所需的一切传递到它.