Friday, July 31, 2015

The method print() is undefined for the type Controller


The method print() is undefined for the type Controller

Problem :


I came across this common exception when i was using mocks for unit testing spring application.

Exception I Encountered Was :


###################################################################################################################

The method print() is undefined for the type Controller

###################################################################################################################

Solution :


When unit testing your spring application with mocks you need to make sure that you are using the neccessary static imports in your code.
See below for the list of static imports to use.
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 

Once i had the static import in place it resolved the exception. Hope this helps resolve your exception, Happy Coding.

Similar Exception :


- How to test a spring controller method by using MockMvc?
- My test helper methods get() status() are undefined
- the method post(string) is undefined for controller test
- Integration testing of spring MVC applicaitons