Mockito模拟为给定的测试用例提供了空值

时间:2020-03-09 17:50:36

标签: spring-boot junit mockito springrunner

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

    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);

    }

    }

但是下面的代码即使在模拟这个之后仍然返回null值,请您帮忙模拟一下。

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

注意:仅Mockito无需使用powermockito

2 个答案:

答案 0 :(得分:1)

对于自动装配字段,您不仅必须对其进行模拟,还应将模拟的类绑定到spring上下文。您有两种选择:

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

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

2。使用@MockBean批注

 @MockBean
 RestClient restClient;

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

答案 1 :(得分:0)

最后,仅使用用户any()而不是anyString()来与Mockito一起使用,因为对象与仅字符串不匹配

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