我们正在集成到 Auth0 API 中,作为整个困惑的一部分,我们有一个函数可以将 UserInfo(来自 Auth0 lib)转换为我们自己的案例类。
该函数的签名是:
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
是公开的。
为什么我不能使用它?
最佳答案
即使该类是 public
,该方法(没有访问修饰符关键字)默认情况下是:
void setValue(...) \\Package-Private
由于您不拥有该包,因此无法以这种方式进行单元测试。此外,从技术上讲,这进入了集成测试领域。
正确的方法是设置一个模拟环境,使用注入(inject)或环境变量来控制使用哪个类,但要获得严格的覆盖可能会很乏味。
关于java - 看似公共(public)功能的不可访问功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863993/