似乎是 public 函数的不可访问函数

Not accessible function of what seem to be a public function

我们正在集成到 Auth0 API 中,作为整个混乱的一部分,我们有一个函数可以将 UserInfo(来自 Auth0 lib)转换为我们自己的案例 class。

这个函数的签名是:

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 class 本身看起来像这样:

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。 为什么我不能使用它?

即使 class 是 public,该方法在没有访问修饰符关键字的情况下默认为:

void setValue(...) \Package-Private

由于您不拥有该包,因此您不能以这种方式进行单元测试。更何况技术上已经进入了集成测试领域。

正确的方法是建立一个模拟环境,使用注入或环境变量来控制使用哪个 class。