NutzCN Logo
精华 service注入在junit中不成功
发布于 66天前 作者 qq_ff330a3f 135 次浏览 复制 上一个帖子 下一个帖子
@IocBean
public class UserModuleTest extends Assert{
    @Inject
    protected Dao dao;
/**
     * Test of login method, of class UserModule.
     */
    @Test
    public void testLogin() {
        System.out.println("login");
        System.out.println(dao+"111111");
        User u = dao.fetch(User.class, Cnd.where("1","=","1"));
        System.out.println(u.toString());
    }

dao始终为空

8 回复

仿照论坛源码的SimpleTest

@wendal 我想仿照来着,但是那个runwith的我这里就没法搞。。。。不知道为什么。。。。

@qq_ff330a3f 论坛的源码里面没这个类的源码吗?

@wendal 有,刚才只是看到test了,我试试看吧,谢谢了

@wendal 我那个nutz里没有这个类,请问这个类是什么?NutTestRunner

下载论坛的源码看

在junit中 Dao dao = Mvcs.getIoc().get(Dao.class);/////这是25行。。。。这个初始化失败了 怎么处理呢?
java.lang.NullPointerException
at cn.wizzer.modules.wf.activiti.ext.CustomGroupEntityManager.(CustomGroupEntityManager.java:25)
at cn.wizzer.modules.SimpleTest.testActivti(SimpleTest.java:62)
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:483)
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:50)
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)

package cn.wizzer.modules.wf.activiti.ext;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;

/**
 * 分组工厂类
 * Created by wizzer on 15-4-27.
 */
@IocBean
public class CustomGroupEntityManager extends GroupEntityManager {
    Dao dao = Mvcs.getIoc().get(Dao.class);/////这是25行。。。。这个初始化失败了 
    private final Log log = Logs.get();

    @Override
    public List<Group> findGroupsByUser(String userId) {
        log.debug("findGroupsByUser:::::::::::::::::::::::::::::::"+userId);

        List<Group> groupList = new ArrayList<Group>();

        Sql sql = Sqls.create("select distinct roleid from sys_user_role where userid=@c");
        sql.params().set("c", userId);
        sql.setCallback(Sqls.callback.records());
        dao.execute(sql);
        List<Record> list = sql.getList(Record.class);
        for (Record m : list) {
            GroupEntity group = new GroupEntity();
            group.setId(Strings.sNull(m.get("roleid")));
            group.setName(Strings.sNull(m.get("roleid")));
            group.setType("assignment");
            group.setRevision(1);
            groupList.add(group);
        }
        return groupList;
    }
}

调用一下 Mvcs.setIoc(ioc);

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