不使用 InjectMocks 创建对象会导致 httpClient 出现问题
Creating objects without using InjectMocks is causing issue for httpClient
我有一个 class,它有一个默认构造函数。在这个构造函数中,许多成员被初始化为:
public classs MyClass{
private String variable1=null;
private String param1="2000";
private String param2="2000";
public MyClass() {
variable1 = getProperty("iaRequestPath");
timeout = Integer.parseInt(getProperty("param1"));
reuqestUrl = getProperty("reuqestUrl");
try {
String maxConnection = getProperty("maxConnection");
int connections =0;
int param3=0;
param3 = validateMethod(maxConnection);//Here parse Int will happen
param4 = validateResource(defaultMaxConnPerRoute);
param5 = validateResource(maxConnPerIAroute);
HttpCoreConnectionFactory factory = new HttpCoreConnectionFactory(parma1, param2, null, 0,
null, "SOME_VALUE");
httpclient = factory.getPooledHttpCoreClient(param3, param4, param5, reuqestUrl);
} catch (Exception exp) {
}
}
如果我使用 @InjectMocks
,我将无法模拟构造函数成员,因为在我的测试 class 中发生模拟之前将调用构造函数。
我避免在我的测试中使用注入模拟和手动创建的对象 class。
喜欢 MyClass testClass=new MyClass();
并像
一样嘲笑 httpClient
HttpClient httpClient =PowerMockito.mock(HttpClient.class);
httpClient always throws null in my actual code since HttpClient is initailized inside constructor .How to
overcome this situation Basically I don't need value of httpclient
from constructor initialization instead I need it from Mock
虽然我嘲笑它没有得到嘲笑的价值。
提前致谢。
我会使用 Powermock 模拟工厂的创建。然后将 getPooledHttpCoreClient
方法模拟为 return httpClient
:
的模拟
@RunWith(PowerMockRunner.class)
@PrepareForTest(HttpCoreConnectionFactory.class)
public class MyClassTest{
@Mock
private HttpCoreConnectionFactory factoryMock;
@Mock
private HttpClient httpClientMock;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
@Test
public void testMethod(){
// Arrange
PowerMockito.whenNew(HttpCoreConnectionFactory.class)
.withArguments(Mockito.anyString(), Mockito.anyString()...).thenReturn(factoryMock);
doReturn(httpClientMock).when(factoryMock)
.getPooledHttpCoreClient(Mockito.anyString(), Mockito.anyString()...);
// prepare the httpClientMock using when() etc..
// Act
MyClass myClass = new MyClass();
// assertions
我有一个 class,它有一个默认构造函数。在这个构造函数中,许多成员被初始化为:
public classs MyClass{
private String variable1=null;
private String param1="2000";
private String param2="2000";
public MyClass() {
variable1 = getProperty("iaRequestPath");
timeout = Integer.parseInt(getProperty("param1"));
reuqestUrl = getProperty("reuqestUrl");
try {
String maxConnection = getProperty("maxConnection");
int connections =0;
int param3=0;
param3 = validateMethod(maxConnection);//Here parse Int will happen
param4 = validateResource(defaultMaxConnPerRoute);
param5 = validateResource(maxConnPerIAroute);
HttpCoreConnectionFactory factory = new HttpCoreConnectionFactory(parma1, param2, null, 0,
null, "SOME_VALUE");
httpclient = factory.getPooledHttpCoreClient(param3, param4, param5, reuqestUrl);
} catch (Exception exp) {
}
}
如果我使用 @InjectMocks
,我将无法模拟构造函数成员,因为在我的测试 class 中发生模拟之前将调用构造函数。
我避免在我的测试中使用注入模拟和手动创建的对象 class。
喜欢 MyClass testClass=new MyClass();
并像
HttpClient httpClient =PowerMockito.mock(HttpClient.class);
httpClient always throws null in my actual code since HttpClient is initailized inside constructor .How to overcome this situation Basically I don't need value of httpclient from constructor initialization instead I need it from Mock
虽然我嘲笑它没有得到嘲笑的价值。
提前致谢。
我会使用 Powermock 模拟工厂的创建。然后将 getPooledHttpCoreClient
方法模拟为 return httpClient
:
@RunWith(PowerMockRunner.class)
@PrepareForTest(HttpCoreConnectionFactory.class)
public class MyClassTest{
@Mock
private HttpCoreConnectionFactory factoryMock;
@Mock
private HttpClient httpClientMock;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
@Test
public void testMethod(){
// Arrange
PowerMockito.whenNew(HttpCoreConnectionFactory.class)
.withArguments(Mockito.anyString(), Mockito.anyString()...).thenReturn(factoryMock);
doReturn(httpClientMock).when(factoryMock)
.getPooledHttpCoreClient(Mockito.anyString(), Mockito.anyString()...);
// prepare the httpClientMock using when() etc..
// Act
MyClass myClass = new MyClass();
// assertions