NutzCN Logo
问答 测试controller遇到NPE
发布于 2817天前 作者 icnws 1558 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

测试代码:

package cn.wizzer;

import cn.wizzer.app.cms.modules.models.Cms_news;
import cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import javax.servlet.http.HttpServletRequest;

import static org.mockito.Mockito.mock;

@RunWith(MyNutTestRunner.class)
@IocBean // 必须有
public class SimpleTest2 extends Assert {
    Log log = Logs.get();

    //private static final Log log = Logs.get();

    // 跟通常的@Inject完全一样.
    @Inject("refer:$ioc")
    protected Ioc ioc;

    @Inject
    protected Dao dao;

    @Inject
    protected CmsNewsController controller;

    @BeforeClass
    public static void beforeX() {
        //这个是必须的。
        MockitoAnnotations.initMocks(SimpleTest2.class);
    }

    @Test
    public void test_create_news() {
        HttpServletRequest request = mock(HttpServletRequest.class);
        Cms_news news = new Cms_news();
        news.setTitle("三胖要革命了~");
        Object object = controller.addDo(news, request);
        log.info("news_obj:" + object.toString());
    }
}

controller方法


@At("/addDo") @Ok("json") @RequiresPermissions("platform.cms.news.add") @SLog(tag = "Cms_news", msg = "${args[0].id}") public Object addDo(@Param("..")Cms_news cmsNews, HttpServletRequest req) { try { cmsNews.setId(R.UU16()); cmsNewsService.insert(cmsNews); return Result.success("system.success"); } catch (Exception e) { return Result.error("system.error"); } }

日志(部分):

[DEBUG] 2017-04-06 08:52:25,857 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO cms_news(id,title,opBy,opAt,delFlag) VALUES(?,?,?,?,?) 
    |                                1 |       2 | 3 |          4 |     5 |
    |----------------------------------|---------|--|------------|-------|
    | 928a5557d3f24e8492f2266c21988905 | 三胖要革命了~ |  | 1491439945 | false |
  For example:> "INSERT INTO cms_news(id,title,opBy,opAt,delFlag) VALUES('928a5557d3f24e8492f2266c21988905','三胖要革命了~','',1491439945,false) "

java.lang.NullPointerException
	at cn.wizzer.framework.base.Result.<init>(Result.java:43)
	at cn.wizzer.framework.base.Result.error(Result.java:60)
	at cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController.addDo(CmsNewsController.java:62)
	at cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController$$NUTZAOP._aop_invoke(CmsNewsController.java:2)
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
	at cn.wizzer.app.web.commons.slog.SLogAopInterceptor.filter(SLogAopInterceptor.java:61)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController$$NUTZAOP.addDo(CmsNewsController.java:1)
	at cn.wizzer.SimpleTest2.test_create_news(SimpleTest2.java:49)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:33)
	at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:14)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

[INFO ] 2017-04-06 08:52:26,010 org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:255) - org.nutz.ioc.impl.NutIoc@2105078741 is closing. startup date [17-04-06 08:52:14.333]
[DEBUG] 2017-04-06 08:52:26,010 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cmsNewsServiceImpl' ...
[DEBUG] 2017-04-06 08:52:26,010 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cmsNewsController' ...
[DEBUG] 2017-04-06 08:52:26,011 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheManager' ...
[DEBUG] 2017-04-06 08:52:26,011 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheProvider' ...

出在框架的类cn.wizzer.framework.base.Result报空指针,不知道怎么解决?
是不是测试的思路有问题?求指点~

9 回复

这是framework框架里的类

package cn.wizzer.framework.base;

import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;

/**
 * Created by wizzer on 2016/12/21.
 */
public class Result {

    private int code;
    private String msg;
    private Object data;

    public Result() {

    }

    public static Result NEW() {
        return new Result();
    }


    public Result addCode(int code) {
        this.code = code;
        return this;
    }

    public Result addMsg(String msg) {
        this.msg = Strings.isBlank(msg) ? "" : Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);
        return this;
    }

    public Result addData(Object data) {
        this.data = data;
        return this;
    }

    public Result(int code, String msg, Object data) {
        this.code = code;
        this.msg = Strings.isBlank(msg) ? "" : Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);
        this.data = data;
    }

    public static Result success(String content) {
        return new Result(0, content, null);
    }

    public static Result success(String content, Object data) {
        return new Result(0, content, data);
    }

    public static Result error(int code, String content) {
        return new Result(code, content, null);
    }

    public static Result error(String content) {
        return new Result(1, content, null);
    }

    public static Result success() {
        return new Result(0, "globals.result.success", null);
    }

    public static Result error() {
        return new Result(1, "globals.result.error", null);
    }

    @Override
    public String toString(){
        return Json.toJson(this,JsonFormat.compact());
    }

}

所以这一句的问题了:

this.msg = Strings.isBlank(msg) ? "" : Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);

改一下, 预防Mvcs.getActionContext().getRequest()是null的情况

fixed:https://github.com/Wizzercn/NutzWk/commit/b98aef646ce9ce68c876c7d01998437ab6b6ddbd

233 有2行 又提交了

多谢,已经解决了

@Wizzercn 你的fix测试不通过,需要改成这样:

/**
     * 在做单元测试的时候Mvcs.getActionContext()及
     * Mvcs.getActionContext().getRequest()会报空指针,这里针对进行处理
     *
     * @param code
     * @param msg
     * @param data
     */
    public Result(int code, String msg, Object data) {
        this.code = code;
        if (Strings.isBlank(msg) || Mvcs.getActionContext() == null || Mvcs.getActionContext().getRequest() == null || Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg) == null) {
            this.msg = "";
        } else {
            this.msg = Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);
        }
        this.msg = Strings.isBlank(msg) ? "" : Mvcs.getActionContext().getRequest() == null ? msg : Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);

        this.data = data;
    }

同时framework的util包的StringUtil这个类需要加一行,判断request是否为空

/**
     * 获得用户远程地址
     */
    public static String getRemoteAddr() {
        HttpServletRequest request = Mvcs.getReq();
        if(request==null) return "";
        String remoteAddr = request.getHeader("X-Real-IP");
        if (Strings.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("X-Forwarded-For");
        } else if (Strings.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("Proxy-Client-IP");
        } else if (Strings.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        String ip = remoteAddr != null ? remoteAddr : Strings.sNull(request.getRemoteAddr());
        if (isIPv4Address(ip) || isIPv6Address(ip)) {
            return ip;
        }
        return "";
    }

上面贴错了一行,fix

public Result(int code, String msg, Object data) {
        this.code = code;
        if (Strings.isBlank(msg) || Mvcs.getActionContext() == null || Mvcs.getActionContext().getRequest() == null || Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg) == null) {
            this.msg = "";
        } else {
            this.msg = Mvcs.getMessage(Mvcs.getActionContext().getRequest(), msg);
        }
        this.data = data;
    }

提交了pull request

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