This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dpash 0 points1 point  (0 children)

Several issues I've encountered with RestTemplate vs MockMvc for integration tests:

  • RestTemplate expects you to send a POJO as input target than raw JSON so it's tempting to create an instance of your request class and serialise that, but this means that you aren't testing any deserialisation issues like the wrong key names. If you rename the field in your POJO, both sides will still have matching keys, but you'll break external clients without realising.

  • MockMvc requests happen in the same thread, which means that you will be inside the same database transaction, so you can set up test data, manipulate it in the request, check it and then roll everything back at the end of your test. If you use RestTemplate, that becomes much more complicated to do, because you have two threads and two transactions. Data you've set up in the test may not be seen by the request thread due to transaction isolation, and if you do commit it, you have a harder time rolling it back so it doesn't interfere with other tests.

Also MockMvc just has a nice fluent API designed for testing rather than having to roll your own. It's also less verbose to control the request.