Перемешивание RestTemplate.exchange () дает URI не абсолютное исключение - PullRequest
0 голосов
/ 07 июня 2018

Я пытаюсь смоделировать этот сервис, который вызывает API, используя шаблон отдыха, и возвращает список.

Я не могу смутить метод restTemplate.exchange().Это дает мне исключение «java.lang.IllegalArgumentException: URI не является абсолютным».

edit- Моя глупая личность забыла поставить http: // перед базовым URL в тестовом примере, и именно поэтому я получил это.Спасибо за помощь и извинения за потраченное время.

Метод для тестирования

@Value("${base-url}")
private String baseUrl;

@Override
public List<Currency> getCurrencyList() {
    RestTemplate restTemplate = new RestTemplate();
    String url = baseUrl + "/currency";
    ResponseEntity<List<Currency>> result;
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> dataHttpEntity = new HttpEntity<>(headers);
    log.info(url);
    result = restTemplate.exchange(url, HttpMethod.GET,dataHttpEntity, new ParameterizedTypeReference<List<Currency>>() {
    });
    return result.getBody();
}

Код тестирования

@Test
public void getCurrencyListTest() {
   ResponseEntity<List<Currency>> result = ResponseEntity.ok(currencyList);

    when(restTemplate.exchange( ArgumentMatchers.anyString(),
            ArgumentMatchers.any(HttpMethod.class),
            ArgumentMatchers.any(),
            ArgumentMatchers.<Class<List<Currency>>>any())).thenReturn(result);
     assertEquals(currencyList,service.getCurrencyList());
}

Исключение

java.lang.IllegalArgumentException: URI is not absolute

at java.net.URI.toURL(URI.java:1088)
at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145)
at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:87)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:721)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:682)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:627)
at com.fpts.seller.service.external.impl.DocumentServiceImpl.getCurrencyList(DocumentServiceImpl.java:34)
at com.fpts.seller.service.external.DocumentServiceImplTest.getCurrencyListTest(DocumentServiceImplTest.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Ответы [ 2 ]

0 голосов
/ 07 июня 2018

Согласно исключению, URL недействителен из-за ArgumentMatcher.anString ().Вы должны предоставить абсолютный URL.

0 голосов
/ 07 июня 2018

Дайте имя протокола для вашего baseUrl и повторите попытку, пожалуйста, я имею в виду под протоколом: http или https.

Например:

String url = "http://" + baseUrl + "/currency";
...