简体   繁体   English

如何在Spring Boot中为RestTemplate编写JUnit测试

[英]How to write a JUnit Test in Spring Boot for RestTemplate

I have never written a JUnit test for a Spring Boot application. 我从未为Spring Boot应用程序编写过JUnit测试。 My Service (localhost) calls via RestTemplate a service, which sends me a response. 我的服务(localhost)通过RestTemplate调用服务,该服务向我发送响应。 Can someone, please, give me a small example with my class structure? 有人可以给我一个关于班级结构的小例子吗? Or does anybody know a good documentation for my case? 还是有人知道关于我的案件的好的文档?

UIController: UIController:

@RequestMapping("/my-service")
public interface MyUIController {

    @RequestMapping(method=RequestMethod.GET, value= "/user", produces="application/json")
    public List<User> getUser(HttpServletRequest request,  HttpServletResponse response);
}

RestController: RestController:

@RestController
public class MyUIRestController implements MyUIController {

  @Autowired
  private MyUIService myUIService;

  public List<User> getUser(HttpServletRequest request,  HttpServletResponse response) {
        try {
          return myUIService.getUser(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

MyUIService: MyUIService:

@Service
public class MyUIService {
   public List<User> getUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String url = this.webServiceProperties.webUserBaseURL+"searchUser";

        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("firstName", request.getParameter("firstName"));

        UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(url).queryParams(params).build().encode();

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
        HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        ResponseEntity<List<User>> responseEntity = restTemplate.exchange(
                uriComponents.toUri(), 
                HttpMethod.GET, requestEntity, 
                new ParameterizedTypeReference<List<User>>() {});

        return responseEntity.getBody();

    }
}

What exactly do I have to test, the service or the RestController ? 我到底要测试什么,服务还是RestController As you can see, I am calling another service. 如您所见,我正在呼叫另一个服务。 Do I need to make mocks or can I test my Controller/Service directly from the service, which I am calling right now? 我需要模拟还是可以直接从我现在正在调用的服务中测试我的Controller / Service?

Thanks in advance! 提前致谢!

It is a good idea to test the RestController , by exposing the getUser() method of your MyUIRestController as an endpoint. 通过将RestControllergetUser()方法MyUIRestController为端点来测试RestController是一个好主意。

You can use Spring MVC Test framework to test your controllers. 您可以使用Spring MVC Test框架来测试您的控制器。 A simple google search for "Spring Rest Controller test" redirected me to the following links: 一个简单的谷歌搜索“ Spring Rest Controller测试”将我重定向到以下链接:

https://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/ https://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/

http://blog.zenika.com/2013/01/15/spring-mvc-test-framework/ http://blog.zenika.com/2013/01/15/spring-mvc-test-framework/

Also you can refer to Spring Documentation below: 您也可以参考下面的Spring文档:

http://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html http://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM