NutzCN Logo
问答 用 mock 测试的时候 ValidationProcessor不执行
发布于 3174天前 作者 qq_cd48ca64 1947 次浏览 复制 上一个帖子 下一个帖子
标签:

用 mock 测试的时候 ValidationProcessor不执行 怎么办

9 回复

大哥,不给点代码,没法帮你找原因啊

//测试的
@Test
public void testList() throws Throwable{
request.setPathInfo("/example/list");
request.addParameter("start", "0");
request.addParameter("limit", "10");
servlet.service(request, response);
System.out.println(response.getAsString());
System.out.println(response.getStatus());
}
//复制 原码里的AbstractMvcTest.java
public class BaseMvcTest {

protected Servlet servlet;

protected MockHttpServletRequest request;

protected MockHttpServletResponse response;

protected MockHttpSession session;

protected MockServletContext servletContext;

protected MockServletConfig servletConfig;

@Before
public void init() throws Throwable {
    servletContext = Mock.servlet.context();
    servletConfig = new MockServletConfig(servletContext, "nutz");
    initServletConfig();
    servlet = new NutServlet();
    servlet.init(servletConfig);

    session = Mock.servlet.session(servletContext);
    newreq();
}

protected void newreq() {
    request = Mock.servlet.request().setSession(session);
    request.setContextPath("");
    request.setSession(session);
    response = new MockHttpServletResponse();
}

protected void initServletConfig(){
    servletConfig.addInitParameter("modules", "com.mxlvniao.upgrade.MainModule");
};

@After
public void destroy() {
    if (servlet != null)
        servlet.destroy();
}

}
//入口方法
@Modules(scanPackage = true)
@Views(CustomViewMaker.class)
@ChainBy(args={"config/processor.json"})
@Fail("http:500")
@Ok("json:full")
//@Filters({@By(type=RequestInterceptor.class)})
@SetupBy(value=MainSetup.class)
public class MainModule {

}

{
validation : {
ps : [
"org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
"org.nutz.mvc.impl.processor.EncodingProcessor",
"org.nutz.mvc.impl.processor.ModuleProcessor",
"!org.nutz.integration.shiro.NutShiroProcessor",
"org.nutz.mvc.impl.processor.ActionFiltersProcessor",
"org.nutz.mvc.impl.processor.AdaptorProcessor",
"com.mxlvniao.common.nuzt.validation.ValidationProcessor",
"org.nutz.mvc.impl.processor.MethodInvokeProcessor",
"org.nutz.mvc.impl.processor.ViewProcessor",
],
error : 'org.nutz.mvc.impl.processor.FailProcessor'
}
}
//启动时 LOG
{
"default" :{
"ps" :["org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor", "org.nutz.mvc.impl.processor.EncodingProcessor", "org.nutz.mvc.impl.processor.ModuleProcessor", "!org.nutz.integration.shiro.NutShiroProcessor", "org.nutz.mvc.impl.processor.ActionFiltersProcessor", "org.nutz.mvc.impl.processor.AdaptorProcessor", "!org.nutz.plugins.validation.ValidationProcessor", "org.nutz.mvc.impl.processor.MethodInvokeProcessor", "org.nutz.mvc.impl.processor.ViewProcessor"],
"error" :"org.nutz.mvc.impl.processor.FailProcessor"
},
"validation" :{
"ps" :["org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor", "org.nutz.mvc.impl.processor.EncodingProcessor", "org.nutz.mvc.impl.processor.ModuleProcessor", "!org.nutz.integration.shiro.NutShiroProcessor", "org.nutz.mvc.impl.processor.ActionFiltersProcessor", "org.nutz.mvc.impl.processor.AdaptorProcessor", "com.mxlvniao.common.nuzt.validation.ValidationProcessor", "org.nutz.mvc.impl.processor.MethodInvokeProcessor", "org.nutz.mvc.impl.processor.ViewProcessor"],
"error" :"org.nutz.mvc.impl.processor.FailProcessor"
}
}

//也是复复GIT上的
package com.mxlvniao.common.nuzt.validation;

import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.impl.processor.AbstractProcessor;

import com.mxlvniao.common.nuzt.validation.annotation.AnnotationValidation;

/**
* 可用于 MVC 效验的动作链
*
* 要求 action 参数中必须有一个 Errors 类型的参数(不需要使用 Param 声明)。当验证完成后会向这个参数赋值
*
* @author QinerG(QinerG@gmail.com)
* @author wendal(wendal1985@gmail.com)
*/
public class ValidationProcessor extends AbstractProcessor {

private static AnnotationValidation av = new AnnotationValidation();

protected boolean hasErrorArg;
public void init(NutConfig config, ActionInfo ai) throws Throwable {
    for (Class<?> klass : ai.getMethod().getParameterTypes()) {
        if (klass.isAssignableFrom(Errors.class)){
            hasErrorArg = true;
        }
    }
}

public void process(ActionContext ac) throws Throwable {
    if (hasErrorArg) {
        Errors es = new Errors();
        for (Object obj : ac.getMethodArgs()) {
            av.validate(obj, es);
        }
        //如果有错误信息就放到请求方法的第一个参数中去
        if(es.hasError()){
            Object[] obj = ac.getMethodArgs();
            obj[0] = es;
        }
    }
    doNext(ac);
}

}

init是会进的
process进不去

mock状态下不ok,那普通状态下呢?

@wendal OK 正常下 是会走的

在入口方法打个断点, 然后在堆栈里面往前找,看看能不能找到这个类

@At("/example/list")
public Object list(Errors err,@Param("..") PageParameter data,@Param("..") Example example,ExpandAdaptorErrorContext errCtx){
if(errCtx!=null){
return new JsonView(422,errCtx.getErrMsgMap());
}
if(err!=null){
return new JsonView(422,err.getListErrorsMap());
}
try {
return new JsonView(200,exampleService.getPageList(data.getStart(),data.getLimit()));
} catch (Exception e) {
e.printStackTrace();
return new JsonView(500);
}
}
这个是要测的方法
ExpandAdaptorErrorContext errCtx 很正常

@wendal 好吧是我自己2了 我给
@Chain("validation") 忘了
sorry

@qq_cd48ca64 找到原因就好

添加回复
请先登陆
回到顶部