goja框架model和controller测试
goja框架model和controller测试
大海无量1 发表于1年前
goja框架model和controller测试
  • 发表于 1年前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: Goja Model Controller TestCase 单元测试

最近在研究model/controller单元测试的问题。

一model测试还好,只要编写测试类 继承ModelTestCase,在此类中的测试方法前加上@Test就可以运行。

二controller测试比较麻烦 当前goja-test中的ControllerTestCase 有泛型检查 ,但是编写测试类继承它的时候有没有泛型参数要加,所以我自己拷贝一份ControllerTestCase 把泛型检查去除了,再运行此类中的测试方法 会在jfinal关闭的时候提示空指针异常 检查发现Cache中cacheimpl没有初始化,又在我的ControllerTestCase中添加Cache.init()代码,重新运行ok。

附上代码:

/*---------------------ControllerTestCase-------------*/

package com.zfdzchina.mytest;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.jfinal.config.JFinalConfig;
import com.jfinal.core.JFinal;
import com.jfinal.handler.Handler;
import goja.Goja;
import goja.core.app.GojaConfig;
import goja.core.cache.Cache;
import goja.core.kits.reflect.Reflect;
import goja.initialize.ctxbox.ClassFinder;
import goja.test.mock.MockHttpRequest;
import goja.test.mock.MockHttpResponse;
import goja.test.mock.MockServletContext;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import javax.servlet.ServletContext;
import org.junit.AfterClass;
import org.junit.Before;

public abstract class ControllerTestCase {
    protected static ServletContext servletContext = new MockServletContext();
    protected static MockHttpRequest request;
    protected static MockHttpResponse response;
    protected static Handler handler;
    private static boolean configStarted = false;
    private static JFinalConfig configInstance;
    private String actionUrl;
    private String bodyData;
    private File bodyFile;
    private File responseFile;
    private Class<Goja> config;

    public ControllerTestCase() {
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        this.config = Goja.class;
    }

    private static void initConfig(JFinal me, ServletContext servletContext, JFinalConfig config) {
        Reflect.on(me).call("init", new Object[]{config, servletContext});
    }

    public static void start(Class<Goja> configClass) throws Exception {
        if(!configStarted) {
            GojaConfig.init();
            ClassFinder.findWithTest();
            Reflect.on(Goja.class).call("initWithTest");
            JFinal me = JFinal.me();
            configInstance = (JFinalConfig)configClass.newInstance();
            initConfig(me, servletContext, configInstance);
            handler = (Handler)Reflect.on(me).get("handler");
            configStarted = true;
            configInstance.afterJFinalStart();
            Cache.init();
        }
    }

    @AfterClass
    public static void stop() throws Exception {
        configInstance.beforeJFinalStop();
    }

    public Object findAttrAfterInvoke(String key) {
        return request.getAttribute(key);
    }

    private String getTarget(String url, MockHttpRequest request) {
        String target = url;
        if(url.contains("?")) {
            target = url.substring(0, url.indexOf("?"));
            String queryString = url.substring(url.indexOf("?") + 1);
            String[] keyVals = queryString.split("&");
            String[] arr$ = keyVals;
            int len$ = keyVals.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String keyVal = arr$[i$];
                int i = keyVal.indexOf(61);
                String key = keyVal.substring(0, i);
                String val = keyVal.substring(i + 1);
                request.setParameter(key, val);
            }
        }

        return target;
    }

    @Before
    public void init() throws Exception {
        start(this.config);
    }

    public String invoke() {
        if(this.bodyFile != null) {
            List resp = null;

            try {
                resp = Files.readLines(this.bodyFile, Charsets.UTF_8);
            } catch (IOException var5) {
                Throwables.propagate(var5);
            }

            this.bodyData = Joiner.on("").join(resp);
        }

        StringWriter resp1 = new StringWriter();
        request = new MockHttpRequest(this.bodyData);
        response = new MockHttpResponse(resp1);
        Reflect.on(handler).call("handle", new Object[]{this.getTarget(this.actionUrl, request), request, response, new boolean[]{true}});
        String response = resp1.toString();
        if(this.responseFile != null) {
            try {
                Files.write(response, this.responseFile, Charsets.UTF_8);
            } catch (IOException var4) {
                Throwables.propagate(var4);
            }
        }

        return response;
    }

    public ControllerTestCase post(File bodyFile) {
        this.bodyFile = bodyFile;
        return this;
    }

    public ControllerTestCase post(String bodyData) {
        this.bodyData = bodyData;
        return this;
    }

    public ControllerTestCase use(String actionUrl) {
        this.actionUrl = actionUrl;
        return this;
    }

    public ControllerTestCase writeTo(File responseFile) {
        this.responseFile = responseFile;
        return this;
    }
}

/*-----------------------------我的测试类------------------------*/

public class ControllerTest extends ControllerTestCase {
    @Test
    public void test()
    {
        String result = use("/myctrl/area/province").invoke();
        System.out.println(result);
        TestCase.assertNotNull(result);
    }
共有 人打赏支持
粉丝 0
博文 1
码字总数 559
×
大海无量1
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: