我正在尝试为使用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);