使用RestTemplate测试方法时出现404错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用RestTemplate测试方法时出现404错误相关的知识,希望对你有一定的参考价值。
我正在尝试为使用RestTemplate下载文件的方法运行单元测试。我已经跟踪了关于主题的多个主题,但与它们相反,当我运行测试时,我得到404错误。
这是我的方法:
@Api(value = "Telecharger fichier sas")
@RestController
@RequestMapping(PUBLIC_BASE_URL)
public class TelechargerFichierRestController extends BaseController {
/** Size of a byte buffer to read/write file */
private static final int BUFFER_SIZE = 4096;
/**Header name */
private static final String HEADER_NAME = "REMOTE_USER";
/**
* Telecharger fichier dans le répertoire download
*
* @param request
* @param response
* @throws Exception
*/
@ApiOperation(value = "Telecharger fichier dans le répertoire download")
@RequestMapping(value = "/sasDl/download", method = RequestMethod.GET)
public void downloadFichier(final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
InputStream inputStream = null;
int bytesRead;
RestTemplate template = new RestTemplate();
// initialisation de headers
HttpHeaders headers = new HttpHeaders();
String remoteUser = request.getHeader(HEADER_NAME);
headers.add(HEADER_NAME, remoteUser);
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
// recupération de ressource depuis psas_m
ResponseEntity<Resource> result = template.exchange(PSASConfig.PREFIX_URL_API_PSAS, HttpMethod.GET, entity,
Resource.class);
// get output stream of the response
OutputStream outStream = response.getOutputStream();
try {
inputStream = result.getBody().getInputStream();
} catch (IOException e) {
throw new IOException(e);
}
byte[] buffer = new byte[BUFFER_SIZE];
// write bytes read from the input stream into the output stream
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
}
}
这是测试:
@RunWith(MockitoJUnitRunner.class)
public class TelechargerFichierRestTest {
@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;
@Mock
private RestTemplate mockRestTemplate;
/**
* RestController
*/
@InjectMocks
private TelechargerFichierRestController restController
/**
* Test method for {@link fr.cnamts.brrsm.services.app.majRequeteSAS.service.impl#DownloadFichier()}.
*
* @throws Exception
*/
@Test
public void testDownloadFichier() throws Exception {
Mockito.doReturn(new ResponseEntity<Resource>(HttpStatus.ACCEPTED))
.when(mockRestTemplate)
.exchange(ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.<HttpEntity<String>> any(),
ArgumentMatchers.<Class<String>> any(),
ArgumentMatchers.<Object>anyVararg());
restController.downloadFichier(request, response);
Mockito.verify(response).getOutputStream();
Mockito.verify(request).getHeader("REMOTE_USER");
}
}
有人能告诉我为什么答案是404,以及如何避免它?谢谢。
答案
你正在嘲笑RestTemplate的5-arg方法
Mockito.doReturn(new ResponseEntity<Resource>(HttpStatus.ACCEPTED)).when(mockRestTemplate)
.exchange(ArgumentMatchers.anyString(), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.<HttpEntity<String>> any(), ArgumentMatchers.<Class<String>> any(), ArgumentMatchers.<Object>anyVararg());
但测试4-arg方法
template
.exchange(PSASConfig.PREFIX_URL_API_PSAS, HttpMethod.GET, entity, Resource.class);
以上是关于使用RestTemplate测试方法时出现404错误的主要内容,如果未能解决你的问题,请参考以下文章
DataTables 在本地工作,托管时出现 404 和 Ajax 错误
在 EmberJS 分发应用程序中直接访问路由时出现错误 404
尝试使用 ErrorDocument 时出现 404 Not Found 错误