NutzCN Logo
问答 写测试代码,报这个org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec错误,数据已经入库
发布于 2781天前 作者 icnws 1773 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

执行test报错org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec,日志如下:

[WARN ] 2017-04-05 14:40:18,718 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:200) - clear cache fail: [cms_news]
java.lang.NullPointerException
	at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.getCache(EhcacheDaoCacheProvider.java:44)
	at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.clear(EhcacheDaoCacheProvider.java:34)
	at org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:195)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:158)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithTransaction(NutDaoRunner.java:104)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:88)
	at org.nutz.dao.impl.sql.run.NutDaoRunner$1.run(NutDaoRunner.java:74)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:72)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:245)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:257)
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:145)
	at cn.wizzer.SimpleTest.test_user_service_create_user(SimpleTest.java:37)
	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.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)

测试类如下

package cn.wizzer;

import cn.wizzer.app.cms.modules.models.Cms_news;
import cn.wizzer.app.cms.modules.services.CmsNewsService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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.lang.random.R;
import org.nutz.log.Log;
import org.nutz.log.Logs;

@RunWith(MyNutTestRunner.class)
@IocBean // 必须有
public class SimpleTest 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 CmsNewsService cmsNewsService;

    @Test
    public void test_user_service_create_user() {
        dao.create(Cms_news.class, false);
        Cms_news news = new Cms_news();
        news.setTitle("我们都很好");
        String id = R.UU16();
        news.setId(id);
        cmsNewsService.insert(news);
        news = dao.fetch(Cms_news.class, id);
        log.info("newsString:"+news.toString());
    }
}

Runner类如下

package cn.wizzer;

import cn.wizzer.app.web.commons.core.Module;
import org.junit.runners.model.InitializationError;
import org.nutz.mock.NutTestRunner;

/**
 * Created by Wayne on 2017/4/5.
 */
public class MyNutTestRunner extends NutTestRunner {
    public MyNutTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    public Class<?> getMainModule() {
        return Module.class;
    }

    /**
     * 可覆盖createIoc,实现参数覆盖, bean替换,等定制.
     *//*
    protected Ioc createIoc() {
        Ioc ioc = super.createIoc();
        PropertiesProxy conf = ioc.get(PropertiesProxy.class, "conf");
        conf.put("db.url", "jdbc:h2:~/test");
        return ioc;
    }*/
}

dao.js配置如下:

5 回复
var ioc = {
		conf : {
			type : "org.nutz.ioc.impl.PropertiesProxy",
			fields : {
				paths : ["config/custom/"]
			}
		},
	    dataSource : {
			factory : "$conf#make",
			args : ["com.alibaba.druid.pool.DruidDataSource", "db."],
	        type : "com.alibaba.druid.pool.DruidDataSource",
	        events : {
	        	create : "init",
	            depose : 'close'
	        }
	    },
		dao : {
			type : "org.nutz.dao.impl.NutDao",
			args : [{refer:"dataSource"}],
			fields : {
				executor : {refer:"cacheExecutor"}
			}
		},
		cacheExecutor : {
			type : "org.nutz.plugins.cache.dao.CachedNutDaoExecutor",
			fields : {
				cacheProvider : {refer:"cacheProvider"},
				cachedTableNames : ["sys_user", "sys_role", "sys_menu"]
			}
		},
		/*
		// 基于内存的简单LRU实现
		cacheProvider : {
			type : "org.nutz.plugins.cache.dao.impl.provider.MemoryDaoCacheProvider",
			fields : {
				cacheSize : 10000 // 缓存的对象数
			},
			events : {
				create : "init"
			}
		}
		*/
		// 基于Ehcache的DaoCacheProvider
		cacheProvider : {
			type : "org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider",
			fields : {
				cacheManager : {refer:"cacheManager"} // 引用ehcache.json中定义的CacheManager
			},
			events : {
				create : "init"
			}
		}
};
public void exec(Connection conn, DaoStatement st) {
        if (!enable || ("true".equals(st.getContext().attr(CacheSkipMark)))) {
            _exec(conn, st);
            return;
        }
        String prepSql = st.toPreparedStatement();
        if (prepSql == null) {
            _exec(conn, st);
            return;
        }
        SQLStatementParser parser = sqlParser(prepSql);
        List<SQLStatement> statementList = null;
        try {
            statementList = parser.parseStatementList();
        }
        catch (Exception e) {
            log.debug("Exception when parser SQL sql, skip cache detect!! SQL=" + prepSql);
            _exec(conn, st);
            return;
        }

我打断点进入CachedNutDaoExecutor看到DaoStatement 的参数st只有RUN这个字符串

仔细看ehcache.js的注释

需要初始化一下ehcache,不然就npe了

解决了

var ioc = {
		cacheManager2 : {
			type : "net.sf.ehcache.CacheManager",
			factory : "net.sf.ehcache.CacheManager#getCacheManager",
			args : ["nutzwk"] // 对应shiro.ini中指定的ehcache.xml中定义的name
		}
		// 如果shiro和Ehcache独立配置使用,使用以下配置参数
		cacheManager : {
			type : "net.sf.ehcache.CacheManager",
			factory : "net.sf.ehcache.CacheManager#create"
		}
};
添加回复
请先登陆
回到顶部