NutzCN Logo
问答 单元测试的时候老是碰到这个问题
发布于 2559天前 作者 一叶孤舟 1751 次浏览 复制 上一个帖子 下一个帖子
标签:

你好:
我在做单元测试的时候,老是报一下错误:
org.nutz.dao.DaoException: java.lang.NullPointerException

at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:139)
at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:92)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:81)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
at org.nutz.dao.impl.NutDao.query(NutDao.java:483)
at org.nutz.dao.impl.NutDao.query(NutDao.java:488)
at com.ft.sms.modules.sys.service.SysUserServiceTest.testQueryUser(SysUserServiceTest.java:82)
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:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: 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.get(EhcacheDaoCacheProvider.java:21)
at org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:160)
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._runWithoutTransaction(NutDaoRunner.java:125)
... 30 more
我把db.js里面:
fields : {
executor : {refer:"cacheExecutor"}
}
这个注释了就不报错,能正常查询,这是什么原因呢?以下是我的db.js的完整配置

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'
          }
        },
        fileSqlManager : {
            type : "org.nutz.dao.impl.FileSqlManager",
            args : "sqlfile"
        },
        dao : {
          type :'org.nutz.dao.impl.NutDao',
          args : [ {refer :"dataSource"},{refer:"fileSqlManager"}],
          fields : {
            executor : {refer:"cacheExecutor"}
          }
        },
        cacheExecutor : {
            type : "org.nutz.plugins.cache.dao.CachedNutDaoExecutor",
            fields : {
                cacheProvider : {refer:"cacheProvider"},
                cachedTableNames : ["sys_user", "sys_role", "sys_menu"]
            }
        },
        // 基于Ehcache的DaoCacheProvider
        cacheProvider : {
            type : "org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider",
            fields : {
                cacheManager : {refer:"cacheManager"} // 引用ehcache.js中定义的CacheManager
            },
            events : {
                create : "init"
            }
        }
};
7 回复

cacheManager的缘故

除了注释掉,还有其他办法吗?

可以去掉cacheManager的js声明,改成如下代码

@IocBean
public class MyBeans {
    @IocBean(name="cacheManager")
    public CacheManager getCacheManager() {
        CacheManager cm = CacheManager.getInstance();
        return cm == null ? CacheManager.createInstance():cm;

    }
}

我ehcache.js是这么写的,应该怎么改呢:

var ioc = {
		cacheManager : {
			type : "net.sf.ehcache.CacheManager",
			factory : "net.sf.ehcache.CacheManager#getCacheManager",
			args : ["sms"] // 对应shiro.ini中指定的ehcache.xml中定义的name
		}
};

把ehcache.js删掉,改成用代码声明

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