NutzCN Logo
问答 做单元测试时,类初始问题
发布于 2774天前 作者 leiwenjie 2036 次浏览 复制 上一个帖子 下一个帖子
标签:

写了一个简单的例子,在做单元测试的时候,报测试类加载异常:

dao.js:

var ioc = {
	dao : {
		type : "org.nutz.dao.impl.NutDao",
		args : [ {
			refer : "dataSource"
		} ]
	},
	dataSource : {
		type : "com.alibaba.druid.pool.DruidDataSource",
		events : {
			depose : "close"
		},
		fields : {
			driverClassName : {
				java : '$conf.get("db_driver")'
			},
			url : {
				java : '$conf.get("db_url")'
			},
			username : {
				java : '$conf.get("db_user")'
			},
			password : {
				java : '$conf.get("db_passwd")'
			},
			filters : "stat",
			initialSize : 5,
			maxActive : 20,
			testOnReturn : true,
			// validationQueryTimeout : 5,
			validationQuery : "select 1"
		}
	},
	userDaoTest : {
		type : "com.leiwenjie.base.support.user.dao.UserDaoTest",
		args : [ {
			refer : "dao"
		} ]
	}
};

TestRunner:

package com.leiwenjie.base.support.base;

import org.junit.runners.model.InitializationError;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;
import org.nutz.mock.NutTestRunner;

/**
 * 
 *
 * @author leiwenjie leiwenjie.cn@gmail.com
 * @version 1.0
 * @date 2017年5月18日 下午10:02:32
 */
public class MyNutTestRunner extends NutTestRunner {

    public MyNutTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    /**
     * 可覆盖createIoc,实现参数覆盖, bean替换,等定制.
     */
    protected Ioc createIoc() {
        Ioc ioc = new NutIoc(new JsonLoader("ioc/"));
        return ioc;
    }
}

testClass:

package com.leiwenjie.base.support.user.dao;

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 com.leiwenjie.base.support.base.MyNutTestRunner;
import com.leiwenjie.base.support.user.entity.UserDo;

@RunWith(MyNutTestRunner.class)
@IocBean
public class UserDaoTest {

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

    @Inject
    protected Dao dao;

    @Test
    public void testSaveUser() {
        Assert.assertNotNull(dao);
        dao.create(UserDo.class, false);
    }
}

日志显示已经连接到数据库:

log4j: 2017-05-18 23:58:45,427 [main] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Name   --> MySQL Connector Java
log4j: 2017-05-18 23:58:45,427 [main] DEBUG org.nutz.dao.impl.DaoSupport - JDBC URL    --> jdbc:mysql://10.211.55.11:3306/basesupport?characterEncoding=UTF8
log4j: 2017-05-18 23:58:45,461 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_client=utf8
log4j: 2017-05-18 23:58:45,461 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_connection=utf8
log4j: 2017-05-18 23:58:45,462 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_database=utf8
log4j: 2017-05-18 23:58:45,462 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_filesystem=binary
log4j: 2017-05-18 23:58:45,462 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_results=
log4j: 2017-05-18 23:58:45,464 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_server=latin1
log4j: 2017-05-18 23:58:45,464 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_set_system=utf8
log4j: 2017-05-18 23:58:45,464 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : character_sets_dir=C:\Home\All\mysql5.6.14\share\charsets\
log4j: 2017-05-18 23:58:45,466 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : binlog_format=STATEMENT
log4j: 2017-05-18 23:58:45,468 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : database=basesupport
log4j: 2017-05-18 23:58:45,468 [main] DEBUG org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert - Mysql : user=basedata@LEIWENJIE
log4j: 2017-05-18 23:58:45,490 [main] DEBUG org.nutz.dao.impl.DaoSupport - Database info --> MYSQL:[MySQL - 5.6.14]
log4j: 2017-05-18 23:58:45,495 [main] DEBUG org.nutz.ioc.impl.ScopeContext - Remove object 'userDaoTest' from [app] 
log4j: 2017-05-18 23:58:45,509 [main] INFO  org.nutz.ioc.impl.NutIoc - org.nutz.ioc.impl.NutIoc@1971525472 is closing. startup date [17-05-18 23:58:43.995]
log4j: 2017-05-18 23:58:45,510 [main] DEBUG org.nutz.ioc.impl.ScopeContext - Depose object 'conf' ...
log4j: 2017-05-18 23:58:45,510 [main] DEBUG org.nutz.ioc.impl.ScopeContext - Depose object 'dao' ...
log4j: 2017-05-18 23:58:45,510 [main] DEBUG org.nutz.ioc.impl.ScopeContext - Depose object 'dataSource' ...
log4j: 2017-05-18 23:58:45,517 [main] INFO  com.alibaba.druid.pool.DruidDataSource - {dataSource-1} closed
log4j: 2017-05-18 23:58:45,517 [main] INFO  org.nutz.ioc.impl.NutIoc - org.nutz.ioc.impl.NutIoc@1971525472 is deposed. startup date [17-05-18 23:58:43.995]

异常信息:

org.nutz.ioc.IocException: IocBean[userDaoTest] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at org.nutz.mock.NutTestRunner.createTest(NutTestRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
	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:678)
	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: org.nutz.lang.born.BorningException: Fail to born or cast to 'com.leiwenjie.base.support.user.dao.UserDaoTest'
 by args: [@(org.nutz.dao.impl.NutDao@3b0dedc9)]
 because: No suitable Constructor or Factory Method!!
	at org.nutz.lang.Mirror.getBorning(Mirror.java:954)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:107)
	... 22 more


是UserDaoTest的初始配置有问题吗

3 回复

删掉dao.js里面的userDaoTest,然后把createIoc方法改成

protected Ioc createIoc() {
        Ioc ioc = new NutIoc(new ComboIocLoader("*js","ioc/","*anno","com.leiwenjie.base"));
        return ioc;
    }

OK,但是为什么不可以在dao.js里面声明呢。是加载顺序的问题吗

@leiwenjie args是构造方法参数,应该用fields

另外,既然要用@IocBean和@Inject,走注解多好,何必每个case都去配daojs呢?

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