无法使用 Mockito 模拟带注释的字段
Cannot mock annotated field using Mockito
以下是被测题目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
注释,以便
它包括需要修改的 class,在您的情况下 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
class 中的字段 tokenService
和 baseUrl
将是 null
。
以下是被测题目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
注释,以便
它包括需要修改的 class,在您的情况下 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
class 中的字段 tokenService
和 baseUrl
将是 null
。