5

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.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • To be honest, I don't think unit testing *this* method is of much worth. The only thing you can really check is that it correctly forwards the parameters to `getFrames` function and then converts each `FrameVariant` to a `Frame`. Both that function and the conversion constructor should unit tested any way. This functionality should be tested as end to end testing, ie deploy this service (and a 'test data set') and make an actual HTTP requests. – thecoshman Aug 19 '15 at 10:14

1 Answers1

9

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.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • @luboskmac, is it alright to use mock mvc for testing rest? – Babu James Oct 20 '15 at 19:05
  • Do you have some objections why it wouldn't? From my point of view, team has to decide what is most effective testing for them. http://stackoverflow.com/a/153565/1919879 – luboskrnac Oct 20 '15 at 21:16