Mockito mock 为给定的测试用例提供 null

Mockito mock is giving null for given test cases

我正在使用 SpringRunner 运行 Junit mockito 测试用例,下面是 class,我试图编写测试用例,但得到空对象

    public class AccountManager {

        public String getToken() throws Exception {
               @Autowired
               RestClient restClient;

                String auth = apiUserPrefix + apiUserName + BatchJobConstants.COLON + apiUserPassword;
                byte[] encodedAuth = Base64.encodeBase64(
                        auth.getBytes(StandardCharsets.UTF_8));
                String authHeader = BatchJobConstants.BASIC_SPACE + new String(encodedAuth);
                String token= null;
                MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
                data.add("grant_type", "client_credential");
                String accManagerUrl = accManagerHost+":"+accManagerPort+"/"+accManagerResPath;
                RestResponseObject responseObject = null;

                try {
                    responseObject = restClient.execute(accManagerUrl, HttpMethod.POST, data, String.class, authHeader);
                    if (responseObject != null && responseObject.getPayload() != null && responseObject.getStatusCode() == HttpStatus.OK) {
                        JsonElement responseJson = (JsonElement) responseObject.getPayload();
                        if (responseJson.isJsonObject()) {
                            token= responseJson.getAsJsonObject().get(BatchJobConstants.ACCESS_TOKEN).getAsString();
                        }catch(RunTimeException e) {
//e
}
    return token;
        }

//Junit测试用例

@RunWith(SpringRunner.class)
    public class AccountManagerTest {
    @InjectMocks
        AccountManager accountManager;
    @Mock
         RestClient restClient;
 @Test
    public void getTokenAccMgrSucess() throws Exception {
        RestResponseObject restResponseObject = Mockito.mock(RestResponseObject.class);

        Mockito.when(restClient.execute(Mockito.anyString(), Mockito.any(HttpMethod.class),
                Mockito.anyString(), Mockito.eq(String.class), Mockito.anyString())).thenReturn(restResponseObject);
        String token = accountManagerTokenProvider.getToken();
        Assert.assertEquals("Token value {} ", null, token);

    }

    }

但是下面的代码 return 即使在 mock 之后仍然是 null 值,请问如何 mock 这个。

responseObject = restClient.execute(accManagerUrl, HttpMethod.POST, data, String.class, authHeader);

注意:只有Mockito不需要使用powermockito

对于自动装配的字段,您不仅需要模拟它,还应该将模拟的 class 绑定到 spring 上下文。您有两个选择:

1。将模拟的 class 标记为主要 bean

@Configuration
    public class TestConfiguration {
        @Bean
        @Primary
        public RestClient restClient() {
            return Mockito.mock(RestClient.class);
        }
    }

2.Use @MockBean注解

 @MockBean
 RestClient restClient;

更多相关信息:
https://www.baeldung.com/injecting-mocks-in-spring
https://www.baeldung.com/java-spring-mockito-mock-mockbean

最终只使用 mockito 用户 any() 而不是 anyString(),因为对象不只与字符串匹配

Mockito.when(restClient.execute(Mockito.any(), Mockito.any(HttpMethod.class),
                Mockito.any(), Mockito.eq(String.class), Mockito.any())).thenReturn(restResponseObject);