在Grails上对JSON内容类型进行单元测试(以及一般情况下)时使用自定义渲染器 [英] Using custom renderer in grails unit testing (and in general) on JSON content types
问题描述
我正在尝试为自定义JSON渲染器获取针对我的REST API中工作的异常的信息.
I am trying to get a custom JSON renderer for exceptions working in my REST api.
我能够获得自定义编组器的工作,该工作完成了我所需的大部分工作,但是我想控制在编组器中无法访问的上下文. grails文档显示了如何编写自定义渲染器,我认为应该可以使用它,但是在对REST控制器进行单元测试时无法使用它.
I was able to get a custom marshaller working that did most of what I needed, but I would like to have control over the context that I don't have access to in the marshaller. The grails documentation shows how to write a custom renderer, and I have one that I think should work, but I can't use it during unit testing my REST controller.
Grails文档: http://grails.org/doc/2.3 .4/guide/webServices.html#customRenderers
Grails docs: http://grails.org/doc/2.3.4/guide/webServices.html#customRenderers
有人知道我将如何初始化此渲染器以用于单元测试期间的控制器动作中?
Does anyone know how I would initialize this renderer to be used in my controller actions during unit testing?
以上文档仅说明了如何在resources.groovy文件中进行设置.
The above documentation only says how to set it up in the resources.groovy file.
使用编组器时,我能够做到:
When I was using the marshaller, I was able to do:
def setup(){
//Set up the custom JSON marshallers
JSON.registerObjectMarshaller(new CusomMarshaller(), 1)
}
但是我看不到Renderers的等效方法.谁能指出我正确的方向?
But I don't see an equivalent method for Renderers. Can anyone point me in the right direction?
更多详细信息:
这是我的渲染器:
class JSONExceptionRenderer extends AbstractRenderer<Exception>{
JSONExceptionRenderer(){
super(Exception, [MimeType.JSON, MimeType.HAL_JSON, MimeType.TEXT_JSON] as MimeType[])
}
@Override
void render(Exception object, RenderContext context) {
log.warn("RENDERING")
Exception exception = (Exception) object
//Default to internal error
Integer code = 500
//If it is a defined exception with a more appropriate error code, then set it
if(exception instanceof RestException){
code = (Integer) ((RestException) exception).getCode()
}else if(exception instanceof MissingResourceException){
code = 404
}
context.status = HttpStatus.valueOf(code)
//Write the JSON
Writer writer = context.getWriter()
Map content = ["code":code, "status":"error", "message":exception.message]
JsonBuilder builder = new JsonBuilder(content)
builder.writeTo(writer)
}
}
这就是我试图使其工作的方式:
And this is the way I am trying to get it to work:
try{
log.info "Throwing exception"
throw new NullPointerException("Test Exception")
}catch(Exception ex){
render ex as JSON
}
谢谢!
推荐答案
如果使用的是spock,则可以直接在Specification
中注入bean.
If you are using spock, you can inject the bean directly in Specification
.
@TestFor(MyController)
class MyControllerSpec extends spock.lang.Specification {
def myCustomRenderer //bean name used in resources.groovy
//continue with tests
}
如果您正在使用junit测试,则可以使用 defineBeans 为:
If you are using junit tests, then you can use defineBeans as:
@TestFor(MyController)
class MyControllerTests {
void setup() {
defineBeans {
myCustomRenderer(com.example.MyCustomRenderer)
}
}
//continue with tests
}
您也可以参考此答案以使用defineBeans.
You can refer this answer as well for use of defineBeans.
我相信这是测试渲染器行为的唯一方法.
I believe this is what you just need to do to test the behavior of the renderer.
这篇关于在Grails上对JSON内容类型进行单元测试(以及一般情况下)时使用自定义渲染器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!