这个是我根据课程里的微信账号的创建修改的一个注册功能,我也是先插入用户表再插入账号,密码,但是还是报空指针异常,DAO层测试都通过了,这个到底是啥原因,老师支个招吧,我弄了好久都没有弄好,太惨了。
这个是LocalAuthServiceImpl的代码
public LocalAuthExecution registeLocalAuth(LocalAuth localAuth) throws LocalAuthOperationException {
// 空值判断
if (localAuth == null || localAuth.getPersonInfo() == null) {
return new LocalAuthExecution(LocalAuthStateEnum.NULL_AUTH_INFO);
}
try {
// 设置创建时间
localAuth.setCreateTime(new Date());
// 如果平台帐号里夹带着用户信息并且用户Id为空,则认为该用户注册账号
// 则自动创建用户信息
if (localAuth.getPersonInfo() != null && localAuth.getPersonInfo().getUserId() == null) {
try {
localAuth.getPersonInfo().setCreateTime(new Date());
localAuth.getPersonInfo().setEnableStatus(1);
PersonInfo personInfo = localAuth.getPersonInfo();
int effectedNum = personInfoDao.insertPersonInfo(personInfo);
localAuth.setPersonInfo(personInfo);
if (effectedNum <= 0) {
throw new LocalAuthOperationException(“添加用户信息失败”);
}
} catch (Exception e) {
log.error(“insertPersonInfo error:” + e.toString());
throw new LocalAuthOperationException("insertPersonInfo error: " + e.getMessage());
}
}
// 注册平台帐号
int effectedNum = localAuthDao.insertLocalAuth(localAuth);
if (effectedNum <= 0) {
throw new LocalAuthOperationException(“注册帐号失败”);
} else {
return new LocalAuthExecution(LocalAuthStateEnum.SUCCESS, localAuth);
}
} catch (Exception e) {
log.error(“insertLocalAuth error:” + e.toString());
throw new LocalAuthOperationException("insertLocalAuth error: " + e.getMessage());
}
}
这个是测试的代码
@Test
public void testABindLocalAuth() {
LocalAuth localAuth = new LocalAuth();
PersonInfo personInfo = new PersonInfo();
String username = “testusername”;
String password = “testpassword”;
// 给平台帐号设置上用户信息
personInfo.setName(“测试一下”);
personInfo.setGender(“女”);
personInfo.setUserType(2);
// 给平台帐号设置用户信息,标明是与哪个用户绑定
localAuth.setPersonInfo(personInfo);
// 设置帐号
localAuth.setUsername(username);
// 设置密码
localAuth.setPassword(password);
// 注册帐号
LocalAuthExecution lae = localAuthService.registeLocalAuth(localAuth);
assertEquals(PersonInfoStateEnum.SUCCESS.getState(), lae.getState());
// 通过userId找到新增的localAuth
localAuth = localAuthService.getLocalAuthByUserId(personInfo.getUserId());
// 打印用户名字和帐号密码看看跟预期是否相符
System.out.println(“用户昵称:” + localAuth.getPersonInfo().getName());
System.out.println(“平台帐号密码:” + localAuth.getPassword());
}
报错信息:
java.lang.NullPointerException
at com.qsf.bysj.service.LocalAuthServiceTest.testABindLocalAuth(LocalAuthServiceTest.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
45行是这里:LocalAuthExecution lae = localAuthService.registeLocalAuth(localAuth);
SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需
了解课程