Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Controller Unittest @RequestMapping

Having a basic Spring Contoller i'd like to Unittest the Request Mapping (not the Method itself), if the doCriticalStuff Method is indeed called

package org.foo;

@Controller
public class HelloWorldController implements IHelloWorldController
{
   @RequestMapping(value = "/b/c/", method = RequestMethod.GET)
   public void doCriticalStuff(HttpServletRequest request, HttpServletResponse response){
      //...
   }
}

Right now I'm doging this via curl -X GET http://myIP:myPort/b/c/ from commandline in a manual way. Any Ideas on how to automate it? I could setup a Jetty instance, send a request and see if i get the expected response but isn't there an easier way provided by Spring?

Related Post: How to test binders/property editors used on spring 2.5 controllers

like image 585
Martin Dürrmeier Avatar asked May 07 '26 03:05

Martin Dürrmeier


1 Answers

I'd use a AnnotationMethodHandlerAdapter and call adapter.handle. Just send in mock request and response objects along with your controller and spring should take care of the rest.

like image 139
stevebot Avatar answered May 08 '26 15:05

stevebot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!