我有以下使用伪装客户端的服务类。我无法对该服务进行单元测试。
Slf4j
@服务 公共类SsoService {
public static final String NO_LINKED_ACCOUNTS_FOUND = "No linked accounts found";
@Autowired
private SsoClient ssoClient;
public String getIdpIdentifier(String accessToken){
log.info("In getUserInfo for token : " + accessToken);
try {
SsoUserInfo ssoUserInfo = ssoClient.getUserInfo(accessToken);
String idpIdentifier = getIdpIdentifier(ssoUserInfo);
System.out.println("idpIdentifier = " + idpIdentifier);
return idpIdentifier;
} catch (Exception e) {
log.error("Error occured while converting token value to Json Object ", e);
throw new UnauthorizedException("Invalid data received");
}
}
这是我的假客户
@FeignClient(name = "ctlSso", configuration = BaseFeignClientConfiguration.class)
公共接口SsoClient {
@GetMapping(value = "/restv1/userinfo")
SsoUserInfo getUserInfo(@RequestParam("access_token") String accessToken);
}
测试如下所示
@RunWith(SpringRunner.class)
public class CtlSsoService_UT {
@MockBean
SsoClient ssoClient;
@InjectMocks
SsoService ssoService;
@Test
public void getIdpIdentifierSuccessful(){
// "createSession end point is called 503dbaa3-6dea-419c-bcd4-59cfc0adf84a, userIdentifier: betaadmin@beta.com and apikey: Basic bXNwLWNsaWVudDpxYS1iR2dSZHFYOTd0VGFWYWs3eDdKbW5yYU4=";
CtlSsoUserInfo userInfo = new CtlSsoUserInfo();
userInfo.setCTLClientToken("123");
userInfo.setCTLClientUid("123");
userInfo.setCTLLastLogin("132");
userInfo.setCTLLastUpdated("123");
userInfo.setCTLMultiFactor("123");
userInfo.setCTLPasswordChange("123");
userInfo.setEmail("123");
userInfo.setFamilyName("123");
userInfo.setGivenName("123");
userInfo.setName("123");
userInfo.setSub("123");
when(ssoClient.getUserInfo(anyString())).thenReturn(userInfo);
String idpIdentifier = ssoService.getIdpIdentifier("503dbaa3-6dea-419c-bcd4-59cfc0adf84a");
}
}
并收到以下错误消息,似乎模拟bean注释未创建SsoClient的实例。 任何指针。
15:15:23.877 [main] INFO SsoService - In getUserInfo for token : 503dbaa3-6dea-419c-bcd4-59cfc0adf84a
15:15:23.879 [main]错误mspauthuiapi.service.SsoService-将令牌值转换为Json对象时发生错误 java.lang.NullPointerException:空