restTemplate.postForEntity 的单元测试导致 ResourceAccessException
Unit test for restTemplate.postForEntity causes ResourceAccessException
我创建了一个方法,只检查端口上 运行 的 url 是否正常工作。
public boolean isBackendRunning() throws URISyntaxException, IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<Object>(headers);
//ResponseEntity<String> out = restTemplate.postForEntity(new URI("https://localhost:8443"), entity, String.class);
ResponseEntity<String> response
= restTemplate.getForEntity("https://localhost:8443", String.class);
return true;
}
问题是 运行 我们的单元测试总是 org.springframework.web.client.ResourceAccessException Caused by: java.net.ConnectException
public static MockWebServer mockBackEnd;
@Autowired Controller Controller;
MissionController mc = Mockito.mock(Controller.class);
@Mock
RestTemplate mockRestTemplate;
@Mock public ResponseEntity<String> responseEntity;
@Test
public void GetRequestTest() throws IOException, URISyntaxException {
Mockito.when(mockRestTemplate.getForEntity(any(), eq(String.class))).thenReturn(responseEntity);
Mockito.when(mock.isBackendRunning()).thenReturn(true);
mockBackEnd.enqueue(new MockResponse().setBody(response).addHeader(content, app_json));
assertEquals(
...
.getMissionJSON(MockOAuth2AuthenticationToken.createAuthCountry())
.toString());
}
在上面的代码示例中,我尝试将 get restTemplate 模拟为 return 结果,并模拟该方法。我认为这会阻止该方法实际上 运行 代码并只给我一个结果,以便我们可以进行单元测试。事实并非如此。
如何在不出现这些错误的情况下处理此检查? OpenConnection 不适用于我们设置程序的方式。我已经尝试了很多不同的方法来让它与 restTemplate 一起工作。
看起来您有一个带有 RestTemplate 依赖项的 MissionController class,并且您想将该依赖项的模拟注入到 MissionController 实例中以对 isBackendRunning 方法进行单元测试。
@RunWith(MockitoJUnitRunner.class)
public class MissionControllerTest {
@Mock
private RestTemplate mockRestTemplate;
@Mock
private ResponseEntity<String> responseEntity;
@InjectMocks
private MissionController missionController = new MissionController();
@Test
public void GetRequestTest() throws Exception {
Mockito.when(mockRestTemplate.getForEntity(any(),
any(Class.class))).thenReturn(responseEntity);
boolean running = missionController.isBackendRunning();
assertTrue(running);
}
}
我创建了一个方法,只检查端口上 运行 的 url 是否正常工作。
public boolean isBackendRunning() throws URISyntaxException, IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> entity = new HttpEntity<Object>(headers);
//ResponseEntity<String> out = restTemplate.postForEntity(new URI("https://localhost:8443"), entity, String.class);
ResponseEntity<String> response
= restTemplate.getForEntity("https://localhost:8443", String.class);
return true;
}
问题是 运行 我们的单元测试总是 org.springframework.web.client.ResourceAccessException Caused by: java.net.ConnectException
public static MockWebServer mockBackEnd;
@Autowired Controller Controller;
MissionController mc = Mockito.mock(Controller.class);
@Mock
RestTemplate mockRestTemplate;
@Mock public ResponseEntity<String> responseEntity;
@Test
public void GetRequestTest() throws IOException, URISyntaxException {
Mockito.when(mockRestTemplate.getForEntity(any(), eq(String.class))).thenReturn(responseEntity);
Mockito.when(mock.isBackendRunning()).thenReturn(true);
mockBackEnd.enqueue(new MockResponse().setBody(response).addHeader(content, app_json));
assertEquals(
...
.getMissionJSON(MockOAuth2AuthenticationToken.createAuthCountry())
.toString());
}
在上面的代码示例中,我尝试将 get restTemplate 模拟为 return 结果,并模拟该方法。我认为这会阻止该方法实际上 运行 代码并只给我一个结果,以便我们可以进行单元测试。事实并非如此。
如何在不出现这些错误的情况下处理此检查? OpenConnection 不适用于我们设置程序的方式。我已经尝试了很多不同的方法来让它与 restTemplate 一起工作。
看起来您有一个带有 RestTemplate 依赖项的 MissionController class,并且您想将该依赖项的模拟注入到 MissionController 实例中以对 isBackendRunning 方法进行单元测试。
@RunWith(MockitoJUnitRunner.class)
public class MissionControllerTest {
@Mock
private RestTemplate mockRestTemplate;
@Mock
private ResponseEntity<String> responseEntity;
@InjectMocks
private MissionController missionController = new MissionController();
@Test
public void GetRequestTest() throws Exception {
Mockito.when(mockRestTemplate.getForEntity(any(),
any(Class.class))).thenReturn(responseEntity);
boolean running = missionController.isBackendRunning();
assertTrue(running);
}
}