【发布时间】:2018-11-24 14:46:12
【问题描述】:
我们正在集成到 Auth0 API 中,作为整个混乱的一部分,我们有一个函数可以将 UserInfo(来自 Auth0 库)转换为我们自己的案例类。
这个函数的签名是:
private[services] def convertUserInfoToAuth0UserInfo(userInfo: UserInfo): Auth0UserInfo
我正在尝试为此函数创建一个单元测试,而对于设置,我需要创建 UserInfo 对象并用数据填充它。
val profileMap = Map(
"email" -> "name@email.com",
"username" -> "username",
"organizationName" -> "organizationName",
"avatarUrl" -> "avatarUrl",
"domains" -> List("domain.com"),
"access_token" -> "access_token"
)
val userInfo = new UserInfo()
userInfo.setValue("key", profileMap.asJava)
val auth0UserInfo = service.convertUserInfoToAuth0UserInfo(userInfo)
auth0UserInfo.accessToken must beSome("access_token")
问题是 setValue 函数由于某种原因无法访问,即使 UserInfo 类本身看起来像这样:
package com.auth0.json.auth;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.HashMap;
import java.util.Map;
/**
* Class that holds the Information related to a User's Access Token. Obtained after a call to {@link com.auth0.client.auth.AuthAPI#userInfo(String)},
* {@link com.auth0.client.auth.AuthAPI#signUp(String, String, String)} or {@link com.auth0.client.auth.AuthAPI#signUp(String, String, String, String)}.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserInfo {
private Map<String, Object> values;
UserInfo() {
values = new HashMap<>();
}
@JsonAnySetter
void setValue(String key, Object value) {
values.put(key, value);
}
/**
* Getter for the values contained in this object
*
* @return the values contained in the object.
*/
@JsonAnyGetter
public Map<String, Object> getValues() {
return values;
}
}
除非我遗漏了什么,否则setValues 是公开的。
为什么我不能使用它?
【问题讨论】:
-
我看到了一个私有包
void setValue(...),这意味着setValues(...)无法工作,因为方法名称不匹配,并且因为您与void setValue(...)不在同一个包中。有关 Java 访问修饰符的信息,请参阅 this 答案。 -
改为使用:
userInfo.getValues().put("https://app.process.st/", profileMap.asJava) -
哈哈!!!显然,出于同样的原因,我什至无法构造对象
-
是的,也是。连那个都没看。
-
那不是很有用,那我们怎么测试这个东西呢?