NutzCN Logo
问答 junit单元测试时开启事物后注入对象异常
发布于 2995天前 作者 忧郁深蓝 2790 次浏览 复制 上一个帖子 下一个帖子
标签:

先贴可以通过的代码
TestBase.java
public class TestBase extends Assert {
protected Ioc ioc;
@Before
public void before() throws Exception {
IocBy iocBy = MainModule.class.getAnnotation(IocBy.class);
IocLoader loader = new ComboIocLoader(iocBy.args());
ioc = new NutIoc(loader);
_before();
}
@After
public void after() throws Exception {
_after();
if (ioc != null) {
ioc.depose();
}
}
protected void _before() throws Exception {
}
protected void _after() throws Exception {
}
}
TestNidBS.java
public class TestNidBS extends TestBase {
private INidBS nidBS;
@Before
public void _init() {
if(nidBS == null)
nidBS = ioc.get(NidBSImpl.class);
try {
Trans.begin();
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void after(){
try {
Trans.rollback();
Trans.close();
} catch (Exception e) {
e.printStackTrace();
}
}

      @Test
      public void test_chinese_var_name() {
         System.out.println(nidBS.getNidNumber("TestKey"));
      }
  }

这样的代码是可以进行查询,修改,插入等操作,并且测试类执行完后,可以正常的回滚
如果改成这样 TestNId.java里的before方法
@Before
public void _init() {
try {
Trans.begin();
} catch (Exception e) {
e.printStackTrace();
}
if(nidBS == null)
nidBS = ioc.get(NidBSImpl.class);
}
则会抛出一个

org.nutz.ioc.IocException: Fail to born 'org.nutz.dao.impl.NutDao'
by args: [
@({
CreateTime:"2016-04-07 10:36:11",
ActiveCount:1,
PoolingCount:0,
CreateCount:1,
DestroyCount:0,
CloseCount:0,
ConnectCount:1,
Connections:[
]
})] becasue:
java.lang.NullPointerException FAIL to create Ioc Bean name=[dao] FAIL to create Ioc Bean name=[nidBSImpl]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:147)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:204)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:229)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:148)
at com.jyjf.common.bs.TestNidBS._init(TestNidBS.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.nutz.ioc.IocException: Fail to born 'org.nutz.dao.impl.NutDao'
by args: [
@({
CreateTime:"2016-04-07 10:36:11",
ActiveCount:1,
PoolingCount:0,
CreateCount:1,
DestroyCount:0,
CloseCount:0,
ConnectCount:1,
Connections:[
]
})] becasue:
java.lang.NullPointerException FAIL to create Ioc Bean name=[dao]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:147)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:204)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:27)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
... 28 more
Caused by: org.nutz.lang.born.BorningException: Fail to born 'org.nutz.dao.impl.NutDao'
by args: [
@({
CreateTime:"2016-04-07 10:36:11",
ActiveCount:1,
PoolingCount:0,
CreateCount:1,
DestroyCount:0,
CloseCount:0,
ConnectCount:1,
Connections:[
]
})] becasue:
java.lang.NullPointerException
at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:19)
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:114)
... 33 more
Caused by: java.lang.NullPointerException
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:35)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
at org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:178)
at org.nutz.dao.impl.NutDao.(NutDao.java:112)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
... 35 more
各位大神看下是什么问题,小弟我还在查找原因中

2 回复

把获取dao的代码放在Trans.begin之前.

也就是想我开始写的那样了,只要nidBS的实例化写在前面,就可以正常!`
我在思考是不是因为dao还没有实例化,就开始事物控制了.所以才导致出现这样的异常!

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