当前位置: 代码迷 >> J2EE >> junit4+springMVC时使用@BeforeClass报错
  详细解决方案

junit4+springMVC时使用@BeforeClass报错

热度:466   发布时间:2016-04-17 23:09:21.0
junit4+springMVC时使用@BeforeClass出错
我在使用junit4+springMVC时:
用Before时正常调用,
@Autowired
private AppPackDao appPackDao;
@Before  
    public void setUp() {  
AppPackEntity appPackEntity = new AppPackEntity();
appPackEntity.setApppackid(apppackid);
appPackEntity.setAppId(appid);
appPackEntity.setPid(pid);
appPackEntity.setChannelId(channelid);
appPackEntity.setSeclevel(1);
appPackDao.insert(appPackEntity);

    }  

但是用BeforeClass时,会报错java.lang.NullPointerException
@Autowired
private static AppPackDao appPackDao;
@BeforeClass  
    public static void setUp() {  
AppPackEntity appPackEntity = new AppPackEntity();
appPackEntity.setApppackid(apppackid);
appPackEntity.setAppId(appid);
appPackEntity.setPid(pid);
appPackEntity.setChannelId(channelid);
appPackEntity.setSeclevel(1);
appPackDao.insert(appPackEntity);

    }  


求大神指点迷津,这是怎么回事?

java.lang.NullPointerException
at com.cmcc.cxb.sec.omss.secret.impl.jk.JyDeviceRegisterTest.setUp(JyDeviceRegisterTest.java:60)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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)

java.lang.NullPointerException
at com.cmcc.cxb.sec.omss.secret.impl.jk.JyDeviceRegisterTest.tearDown(JyDeviceRegisterTest.java:130)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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)
------解决思路----------------------
@BeforeClass时候还没注入吧大概...
------解决思路----------------------
在@beforeclass的时候你需要初始化spring容器,让spring容器创建对应的bean对象,包括你引用到的appPackDao这个东西,spring容器都没有初始化,你想干什么呢?还想去让spring给你注入你想要的对象,门都没有。
  相关解决方案