使用springBoot 编写测试用例的时候,想要实现对象注入,但是当使用@Autowired
@Autowired
private UserDomainService userDomainService;
这种方式注入的使用会出现空指针,
解决办法
@SpringBootTest
@RunWith(SpringRunner.class)
如下:
package *.test;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import *
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class HMacTest {
@Test
public void test() {
String testStr = "GET\n" + "null\n" + "1618999321000\n" + "/app/application/permission/CheckAuthorization?key=2232&name=wei";
byte[] key = "ed209hN8b45N1Z3nB687Mr39V32tujjE".getBytes();
HMac mac = new HMac(HmacAlgorithm.HmacSHA1, key);
String macHex1 = mac.digestHex(testStr);
System.out.println(macHex1);
}
@Autowired
private UserDomainService userDomainService;
@Test
public void qry(){
User queryUserByPhone = new User();
queryUserByPhone.setLoginPhone("1*********7");
List<User> userList = userDomainService.list(queryUserByPhone);
User user = null;
for (User userInfo : userList) {
if (UserStatusEnum.STATUS_1.getCode().equals(userInfo.getStatus()) && UserStatusEnum.STATUS_1.getCode().equals(userInfo.getStatusUbp()) &&
StrUtil.isNotBlank(userInfo.getUserRole())) {
user = userInfo;
break;
}
}
System.out.println("根据手机号查询用户信息返回信息: {}"+ user);
}
}
这样在执行qry() 方法的时候 userDomainService 就避免了 空指针的问题
|