Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Tuesday, June 30, 2015

The method post(string) is undefined for controller test


The method post(string) is undefined for controller test

Problem :


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

Exception I Encountered Was :


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

the method post(string) is undefined for controller test

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

Solution :


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

Once i had the static imports 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
- the method print() is undefined for the type Controller


Wednesday, January 22, 2014

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed

Problem :


We come across this common exception when using Java Spring application.

Exception I Encountered Was :


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

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4660)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
###################################################################################################################

Solution :


This exception was caused when i did not add the maven dependencies to the build path. I was using eclipse with maven, so had to include the maven dependencies in the build path as explained below.

Example :


- Adding Maven dependencies in project web deployment assembly : 
          
  • Open project properties(example: In project explorer right Click on project name to select "Properties").
  • Select "Deployment Assembly".
  • Click "Add..." button on the right navigation.
  • Select "Java Build Path Entries" from menu of Directive Type and then click "Next".
  • Select "Maven Dependencies" from Java Build Path Entries menu and then click "Finish".
  • Now the "Maven Dependencies" should be added to the Web Deployment Assembly.
- When using Eclipse WDT and using unmodified and previously working project :
  • If Tomcat is running Stop it
  • Open the "Servers" view: Window then select "Show view" and then select "Other" > Server > Servers
  • Right Click on the Tomcat Server and then select "Clean Tomcat Work Directory".
  • Right Click on the Tomcat Server and then select "Clean"
  • Restart the Tomcat Server
The above solution should resolve the exception.

Similar Exception :

     
- ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener