UserDao:
@IocBean
public class UserDao implements IUserDao {
@Inject(optional = true)
protected Dao dao;
@Override
public User insert(User t) {
return dao.fastInsert(t);
}
}
user.js:
var ioc = {
userDao : {
type : 'com.leiwenjie.base.support.user.dao.impl.UserDao',
singleton : true,
args : [ {
refer : "dao"
} ]
}
};
MyNutTestRunner:
public class MyNutTestRunner extends NutTestRunner {
public MyNutTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
/**
* 可覆盖createIoc,实现参数覆盖, bean替换,等定制.
*/
protected Ioc createIoc() {
Ioc ioc = null;
try {
ioc = new NutIoc(new ComboIocLoader("*js", "ioc/", "*anno", "com.leiwenjie.base.support.base",
"com.leiwenjie.base.support.user.dao"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return ioc;
}
}
UserDaoTest:
@RunWith(MyNutTestRunner.class)
@IocBean
public class UserDaoTest {
@Inject
protected IUserDao userDao;
@Test
public void testSaveUser() {
User user = new User();
user.setLoginName("zhangsan");
user.setPassword("abc123");
user.setType(User.USER_REGIST_TYPE_LOCAL);
user.setStatus(SupportConstants.CONS_STR_YES);
user.setRegisterTime(new Date());
userDao.insert(user);
Assert.assertTrue(user.getId() != 0);
}
}
异常信息:
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: java.lang.RuntimeException: IocBean[userDaoTest] fail at field=[userDao]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
... 22 more
Caused by: org.nutz.ioc.IocException: IocBean[userDao] 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.val.ReferTypeValue.get(ReferTypeValue.java:60)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 24 more
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'com.leiwenjie.base.support.user.dao.impl.UserDao'
by args: [@(org.nutz.dao.impl.NutDao@27e54167)]
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)
... 27 more