Announcement Announcement Module
Collapse
No announcement yet.
spring-test-mvc not finding my controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-test-mvc not finding my controller

    Hello,

    I'm trying to to test a sample mvc application with spring-test-mvc (version 1.0.0.BUILD-SNAPSHOT and spring frameworrk 3.1.0.BUILD-SNAPSHOT), but I'm having troubles calling the controller using the MockMvcBuilders.xmlConfigSetup.

    The controller is very simple:

    Code:
    @Controller
    public class MyController {
    
    	@RequestMapping("/hello")
    	public @ResponseBody String hello(String name) {
    		return String.format("Hello, %s!", name != null ? name : "World");
    	}
    }
    If I run the test with the MockMvcBuilders.standaloneSetup, everything works fine:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:application-context.xml"})
    public class IntegrationTest {
    
    	@Autowired
    	private MyController mycontroller;
    
    	private MockMvc mockMvc;
    
    	@Before
    	public void setup() {
    		mockMvc = MockMvcBuilders.standaloneSetup(mycontroller).build();
    	}
    
    	@Test
    	public void testHelloWorld() throws Exception {
    		mockMvc.perform(get("/hello"))
    			.andExpect(status().isOk())
    			.andExpect(content().string("Hello, World!"));
    	}
    
    }
    When I try to use the MockMvcBuilders.xmlConfigSetup instead i get a 404 from the dispatcher:

    Code:
    public class IntegrationTest {
    
    	private MockMvc mockMvc;
    
    	@Before
    	public void setup() {
    		mockMvc = MockMvcBuilders.xmlConfigSetup("classpath:application-context.xml").build();
    	}
    
    	@Test
    	public void testHelloWorld() throws Exception {
    		mockMvc.perform(get("/hello"))
    			.andExpect(status().isOk())
    			.andExpect(content().string("Hello, World!"));
    	}
    
    }
    This is the error detail:

    Code:
    ------------- Standard Error -----------------
    Nov 28, 2011 3:15:54 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing Root WebApplicationContext: startup date [Mon Nov 28 15:15:54 CET 2011]; root of context hierarchy
    Nov 28, 2011 3:15:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [application-context.xml]
    Nov 28, 2011 3:15:54 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4865ce: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,myController,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
    ------------- ---------------- ---------------
    Testcase: testHelloWorld(IntegrationTest):	FAILED
    Status expected:<200> but was:<404>
    junit.framework.AssertionFailedError: Status expected:<200> but was:<404>
    	at org.springframework.test.web.AssertionErrors.fail(AssertionErrors.java:46)
    	at org.springframework.test.web.AssertionErrors.assertEquals(AssertionErrors.java:75)
    	at org.springframework.test.web.server.result.StatusResultMatchers$3.matchResponse(StatusResultMatchers.java:471)
    	at org.springframework.test.web.server.result.ResultMatcherAdapter.match(ResultMatcherAdapter.java:43)
    	at org.springframework.test.web.server.MockMvc$1.andExpect(MockMvc.java:88)
    	at IntegrationTest.testHelloWorld(IntegrationTest.java:33)
    What am I doing wrong? I must be missing something pretty obvious.


    Thanks,
    Paolo

  • #2
    You should initialize servlet-context, probably you don't

    Code:
    @WebAppConfiguration
    @ContextConfiguration(locations = { "classpath:test-context.xml",
    		"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml" })
    @RunWith(SpringJUnit4ClassRunner.class)
    public class UserControllerTest extends AbstarctAutenticatedControllerTest
    {

    Comment

    Working...
    X