简体   繁体   中英

Spring Junit test case for controller rest service

I have a controller which responds to REST calls, I have various test cases for my other public methods.

I do not know how to write one for my controller:

@RequestMapping(value = "/api/frames", method = RequestMethod.GET)
public List<Frame> getFrames(
  @RequestParam(value="frameLength", required=true) Double frameLength,
  @RequestParam(value="frameBreadth", required=true) Double frameBreadth,
  @RequestParam(value="mountThickness", required=true) Double mountThickness,
  @RequestParam(value="frameThickness", required=true) Double frameThickness){
    List<Frame> tempFrames = new ArrayList<>();
    List<FrameVariant> frameVariants = frameVariantService.getFrames(
      frameLength, frameBreadth, mountThickness, frameThickness);
    for (FrameVariant frameVariant : frameVariants) {
      tempFrames.add(new Frame(frameVariant));
    }
    return tempFrames;
  }

I have no clue how to write a test case for this controller method.

Take a look at MockMvc . It's part of Spring Test module.

These tutorials are pretty descriptive and going into details so you should get idea straight away how to test Spring MVC controllers.

This article provides a good introduction to testing REST controllers with MockMvc. Sample code for this post is available on Github.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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