无法模拟RESTTEMPLATE响应
我想模拟RESTTEMPLATE的结果,但是在下面的代码下,它总是进行HTTP。有人可以建议吗?我想我有错误的部分,请帮忙。
userviceTest.java @runwith(springjunit4classrunner.class)
public class UserServiceTest(){
@InjectMock
@Autowired
UserService userservice;
@Mock
RestTemplate restTemplate;
@Value(${aa.bb.cc})
private String getfromapplicationpropertiesVal;
@Test
public void test1(){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String jsonBody = null;
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
String textContent = "result from junit";
ResponseEntity<String> response = new ResponseEntity<>(textContent, HttpStatus.OK);
String url = "http://localhost:8080/test/test.txt";
doReturn(response).when(restTemplate).exchange(
eq(url),
any(HttpMethod.class),
any(HttpEntity.class),
any(Class.class)
);
userservice.test();
}
}
uservice.java
@Service
public class UserService{
@Autowired
HttpHelperService httpHelperService;
public void test(){
String url = "http://localhost:8080/test/test.txt";
String response = httpHelperService.cal(url, HttpMethod.GET);
System.out.println(response);
}
}
httphelperservice.java
@Service
public class HttpHelperService{
@Autowired
private RestTemplate restTemplate;
public String cal(String url, HttpMethod httpMethod){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String jsonBody = null;
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
String response = restTemplate.exchange(url, httpMethod, entity, String.class); //This part kept calling http when run the @Test
}
}
restTemplateConfig
@Configuration
public class RestTemplateConfig{
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
I wanted to mock the RestTemplate's result, but with my code below, it always went to do the Http. Can someone please advise? I think I have parts that I did wrongly, please help.
UserServiceTest.java
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceTest(){
@InjectMock
@Autowired
UserService userservice;
@Mock
RestTemplate restTemplate;
@Value(${aa.bb.cc})
private String getfromapplicationpropertiesVal;
@Test
public void test1(){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String jsonBody = null;
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
String textContent = "result from junit";
ResponseEntity<String> response = new ResponseEntity<>(textContent, HttpStatus.OK);
String url = "http://localhost:8080/test/test.txt";
doReturn(response).when(restTemplate).exchange(
eq(url),
any(HttpMethod.class),
any(HttpEntity.class),
any(Class.class)
);
userservice.test();
}
}
UserService.java
@Service
public class UserService{
@Autowired
HttpHelperService httpHelperService;
public void test(){
String url = "http://localhost:8080/test/test.txt";
String response = httpHelperService.cal(url, HttpMethod.GET);
System.out.println(response);
}
}
HttpHelperService.java
@Service
public class HttpHelperService{
@Autowired
private RestTemplate restTemplate;
public String cal(String url, HttpMethod httpMethod){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String jsonBody = null;
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);
String response = restTemplate.exchange(url, httpMethod, entity, String.class); //This part kept calling http when run the @Test
}
}
RestTemplateConfig
@Configuration
public class RestTemplateConfig{
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这些类型的方案中建议使用 oigrestserviceserver &amp;不模拟
RESTTEMPLATE
。您可能会执行类似的操作:
然后为您的服务调用模拟,例如:
- 替代,您只需模拟
test()
方法:Recommendation in these type of scenario is to use MockRestServiceServer & not mocking
restTemplate
.You might do something like this:
And then invoke mock for your service something like :
-Alternatively, you can just mock your
test()
method: