Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing REST Controller with spring-test-mvc
    primarykey
    data
    text
    <p>I have upgraded my Spring dependency to Spring 3.1.1.RELEASE and I'm trying to use <a href="https://github.com/SpringSource/spring-test-mvc" rel="nofollow noreferrer">spring-test-mvc</a> to unit test a simple Controller. I have been following the technique used in <a href="https://stackoverflow.com/questions/9155527/spring-rest-controller-test-with-spring-test-mvc-framework">Spring REST Controller Test with spring-test-mvc framework</a>, since it seems to have worked for that person, but I have been unsuccessful so far. I think there's some key configuration I"m missing in my test context file.</p> <p>I get no errors. The reason I know it's not working is because <code>Hello World</code> never gets printed (see Controller). What am I missing here?</p> <p><strong>Controller:</strong></p> <pre><code>@Controller @RequestMapping("/debug") public class DebugOutputController { @RequestMapping(method = RequestMethod.POST) public void saveDebugOutput(@RequestBody DebugOutput debugOutput, HttpServletResponse response) { System.out.println("Hello World"); } } </code></pre> <p><strong>Test Class:</strong></p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) //this lets tests access Spring beans defined in the context config file @ContextConfiguration(locations={"file:src/test/resources/itest/restAPITestContext.xml"}) //tells the test where to get configuration and beans to be used by the test. @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) //overrides the default stack of listeners public class ITRestAPI{ @Autowired private DebugOutputController debugOutputController; private MockMvc mockMvc; @Before public void setUp() throws Exception { mockMvc = MockMvcBuilders.standaloneSetup(debugOutputController).build(); } @After public void tearDown() throws Exception { } @Test public void shouldPerformPost() throws Exception { this.mockMvc.perform(post("/debug")); } } </code></pre> <p><strong>restAPITestContext.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;context:component-scan resource-pattern="*DebugOutputController*" base-package="com.company.project.servlet" /&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload