gpt4 book ai didi

java - 无法使用 Mockito 模拟带注释的字段

转载 作者:行者123 更新时间:2023-12-02 08:49:53 26 4
gpt4 key购买 nike

以下是测试的主题RestClient.java

package com.demo.mockito;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.demo.sample1.RestClient;
import com.demo.sample2.AboutApi;
import com.demo.sample3.ServiceInfo;
import com.demo.sample4.FeatureRepo;

@Component
@Slf4j
public class RestClient {

@Value("${test.api.baseURL:http://localhost:80}")
private String baseURL;

private static String ACTIVE = "ACTIVE";

@Autowired(required = false)
private TokenService tokenService;

private FeatureRepo featureRepo;

RestClient(FeatureRepo featureRepo) {
this.FeatureRepo = featureRepo;
}

public boolean isEnabled() {
AboutApi aboutApi = new AboutApi(getApiClient());
ServiceInfo serviceInfo = aboutApi.getMultiSiteServiceInfo();
Validate.notNull(serviceInfo);
return ACTIVE.equals(serviceInfo.getStatus());
}

private ApiClient getApiClient() {
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(baseURL);
return apiClient;
}
}

这是测试RestClientTest.java

package com.demo.mockito;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import com.demo.sample1.RestClient;
import com.demo.sample2.AboutApi;
import com.demo.sample3.ServiceInfo;
import com.demo.sample4.FeatureRepo;


@RunWith(PowerMockRunner.class)
public class RestClientTest {
@InjectMocks private RestClient restClient;

@Mock private AboutApi aboutApiClient;

@Mock ServiceInfo serviceInfo;

@Mock FeatureRepo featureRepo;

@Before
public void init() throws ApiException {
when(aboutApiClient.getServiceInfo()).thenReturn(serviceInfo);
when(serviceInfo.getStatus()).thenReturn("ACTIVE");
}

@Test
public void testIsEnabled() throws ApiException {
boolean status = restClient.isEnabled();
assertTrue(status);
}
}

当我运行测试时,理想情况下,当它到达 RestClient.java 的 isEnabled 方法的第二行时,应该按照 @Before 第一行中的说明模拟输出但它尝试调用导致 IllegalArgumentException 的真实方法。

有人可以告诉我如何正确模拟该调用而不对文件 RestClient.java 进行任何更改吗?

最佳答案

编辑:已更新

I have issue in the line AboutApi aboutApi = new AboutApi(getApiClient()). Here it is calling a new instance instead of using the one which I mocked. I want to know how can I insert my mocked instance of AboutApi without touching RestClient.java

在这种情况下,您必须查看 PowerMockito 的 whenNew 功能。

您必须添加@PrepareForTest注释,以便它包含需要修改的类,在您的情况下是 RestClient

@RunWith(PowerMockRunner.class)
@PrepareForTest(RestClient.class)

不确定您的问题中是否存在拼写错误,但您需要定义行为的方法应该是 getMultiSiteServiceInfo 而不是 getServiceInfo

@Test
public void testIsEnabled() throws Exception {

Mockito.when(aboutApiClient.getMultiSiteServiceInfo()).thenReturn(serviceInfo);
Mockito.when(serviceInfo.getStatus()).thenReturn("ACTIVE");

PowerMockito.whenNew(AboutApi.class)
.withAnyArguments()
.thenReturn(aboutApiClient);

boolean status = restClient.isEnabled();
Assert.assertTrue(status);
}

如果您想更具体,可以将 withAnyArguments() 替换为 withArguments(Mockito.any(ApiClient.class))

但请注意,RestClient 类中的 tokenServicebaseUrl 字段将为 null

关于java - 无法使用 Mockito 模拟带注释的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60848614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com