5
回答
spring+junit中报错?求指正
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
java.lang.NullPointerException
at com.silence.online.service.admin.AdminServiceTest.testLogin(AdminServiceTest.java:59)
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: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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
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: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)


举报
共有5个答案 最后回答: 1年前

引用来自“石头先森”的评论

总要贴源代码上来看看吧,空指针也是有原因的
service层代码:
public List<Admin> login(String loginName){
                return adminDao.queryAdminByLoginName(loginName,Short.parseShort("1"));
dao层代码:
        public List<Admin> queryAdminByLoginName(String loginName,short adminStatus){
                String hql="From Admin";
                hql+=" where loginName = ? and adminStatus= ?";
                Query query=getSession().createQuery(hql);
                query.setString(0, loginName);
                query.setShort(1, adminStatus);
                return (List<Admin>)query.list();

        } 

就这段我在junit中测试时,就空指针、请问该怎么写测试?

--- 共有 3 条评论 ---
我就只想静静努力回复 @石头先森 : 调试出是admindao 为null 1年前 回复
我就只想静静努力回复 @石头先森 : 新手啦。 我百度试试 . 1年前 回复
ALAI丶大兄弟会不会断点调试? 1年前 回复

引用来自“哎码”的评论

java.lang.NullPointerException
at com.silence.online.service.admin.AdminServiceTest.testLogin(AdminServiceTest.java:59)


这么老大的空指针异常

源码如下,请问大神该怎么单元测试?

service层代码:

public List<Admin> login(String loginName){
                return adminDao.queryAdminByLoginName(loginName,Short.parseShort("1"));
dao层代码:
        public List<Admin> queryAdminByLoginName(String loginName,short adminStatus){
                String hql="From Admin";
                hql+=" where loginName = ? and adminStatus= ?";
                Query query=getSession().createQuery(hql);
                query.setString(0, loginName);
                query.setShort(1, adminStatus);
                return (List<Admin>)query.list();

        } 

顶部