我正在为使用其余模板的方法编写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);
    }


我使用模仿框架编写的上述方法的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));
}


}


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

我嘲笑了对象,但仍然失败,并出现异常

最佳答案

嘲笑的问题在于RestTemplate是StudentClient中的私有字段,而StudentClient没有此私有字段RestTemplate的构造函数/设置程序。模拟注入当前无提示地失败。

如果通过构造器/设置器公开此字段,则模拟将正确注入到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/

10-10 12:47