gpt4 book ai didi

java - Junit 测试用例中未知主机的 GET 请求发生 I/O 错误

转载 作者:行者123 更新时间:2023-12-01 09:46:47 25 4
gpt4 key购买 nike

我正在为使用rest模板的方法编写Junit测试用例,下面是方法。

public Student getStudent(Integer studId) throws Exception {
RestTemplate restTemplate = new RestTemplate(
utility.setAuthenticationParameters(
"test", "test"));
String url=https://dev.test.res.com/student;
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<>(headers);
url =url+"?"+studId;
Map<String, String> params = new HashMap<>();
params.put("studId", studId);
ResponseEntity<String> response = restTemplate.exchange(url,
HttpMethod.GET, entity, String.class, params);
}

我使用mockito框架编写的上述方法的Junit测试用例

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URI;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.datatype.DatatypeConfigurationException;

import junit.framework.Assert;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;


@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = {ConfigData.class})
public class StudentClientTest {

@Mock
RestTemplate restTemplate;

@Mock
ClientHttpRequestFactory requestFactory;

@Mock
ClientHttpRequest request;

@Mock
MockClientHttpRequest mockClientHttpRequest;

@Mock
ClientHttpResponse response;

@Mock
ResponseEntity<String> responseEntory;

private MockRestServiceServer mockServer;


@InjectMocks
private StudentClient studentClient= new StudentClient();



@Before
public void setUp() {
MockitoAnnotations.initMocks(this);

}


@Test
public void getStudentTest() throws Exception{



String uri = "https://dev.test.res.com/student?";

Integer studId=100;
uri=uri+studId;

Mockito.when(restTemplate.exchange(Mockito.contains(uri),Mockito.eq(HttpMethod.GET),Mockito.isA(HttpEntity.class), Mockito.eq(String.class),Mockito.isNotNull())).thenReturn(responseEntory);
assertNotNull(studentClient.getStudent(100));
}


}

https://dev.test.res.com/student?studId=100 ”的 GET 请求发生 I/O 错误:嵌套异常为 java.net.UnknownHostException:dev.test.res.com

我已经模拟了该对象,但仍然失败并出现异常

最佳答案

您的模拟问题是 RestTemplate 是 StudentClient 内部的私有(private)字段,而 StudentClient 没有此私有(private)字段 RestTemplate 的构造函数/ setter 。模拟注入(inject)目前静默失败。

如果您通过构造函数/setter 公开此字段,则模拟将被正确注入(inject)到 StudentClient 中。

更多详细信息请参见:https://tedvinke.wordpress.com/2014/02/13/mockito-why-you-should-not-use-injectmocks-annotation-to-autowire-fields/

关于java - Junit 测试用例中未知主机的 GET 请求发生 I/O 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938997/

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