NutzCN Logo
问答 Junit测试service,用到getEntityClass() 返回的type 都是null
发布于 2765天前 作者 chafferer 3475 次浏览 复制 上一个帖子 下一个帖子
标签:
@RunWith(RaiisIocTestRunner.class)
@IocBean
public class UserTest extends Assert {

	@Inject("refer:$ioc")
	protected Ioc ioc;

	@Test
	public void test_service_ok() {
		UserService userService = ioc.get(UserService.class);
		User user = userService.fetch(1);
		assertNotNull(user);
	}
	
	
}

Service 里面的方法:

	@Override
	public User fetch(int id) {
		if (dao() == null) {
			System.out.println(1);
		}
		User user = dao().fetch(getEntityClass(), Cnd.where("id", "=", id));
		if (!Lang.isEmpty(user)) {
			dao().fetchLinks(user, "roles");
		}
		return user;
	}
15 回复

debug一下UserService的构造方法

UserService 没有构造方法啊

进的是第一个方法

    /**
     * @see IdEntityService
     * @see NameEntityService
     */
    public IdNameEntityService() {
        super();
    }

    /**
     * @see IdEntityService
     * @see NameEntityService
     */
    public IdNameEntityService(Dao dao) {
        super(dao);
    }

    /**
     * @see IdEntityService
     * @see NameEntityService
     */
    public IdNameEntityService(Dao dao, Class<T> entityType) {
        super(dao, entityType);
    }

在EntityService的构造方法内

mirror 的klass 有值,但是type =null

type是啥东西

getEntityClass调用的是mirror.getType(),而mirror.getType()返回的是klass

你是说klass有值但mirror.getType()返回null?这不可能吧

好吧,原来getType() 返回的是class ,我以为是mirror的type属性。。。

对了,兽总, 那我运行junit测试的时候,这句话老是报空指针异常

User user = dao().fetch(getEntityClass(), Cnd.where("id", "=", id));

我检查了,这个dao() 也不是null 啊

确定你能看到那个数字1,dao()为null的时候

我猜你要的是这个fields

@IocBean(fields="dao")

我的userService 跟 nutzbook里面的不太一样,我还是写成了接口的方式

@IocBean(name="userService", fields="dao")
public class UserServiceImpl extends IdNameEntityService<User> implements UserService  { 
。。。
}

junit 运行到这句话会报这个异常

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:248)
	at org.nutz.dao.impl.NutDao.fetch(NutDao.java:556)
	at com.panport.raiis.nutz.service.impl.UserServiceImpl.fetch(UserServiceImpl.java:83)
	at com.panport.raiis.UserTest.test_service_ok(UserTest.java:26)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
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:159)
	at org.nutz.plugins.cache.dao.DaoCacheInterceptor.filter(DaoCacheInterceptor.java:18)
	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)
	... 31 more

原来是ehcache的问题,在before里面加一句

net.sf.ehcache.CacheManager.create();

刚好我也看到了...
已经写了一天一夜的代码了,不好意思脑袋有坑。。。

原来是ehcache的问题,在before里面加一句
net.sf.ehcache.CacheManager.create();
这句话是什么意思?
我调用getEntityClass(),返回的是:
class java.lang.Object

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