4

I am trying to create Test Cases for a RestController. Here is a simple RestContorller

@RestController
@RequestMapping("/media")
public class MediaListController {
    @RequestMapping(method = RequestMethod.GET)
    public String getAllMedia(){
        return "TEST COMPLETE";
    }
}

And I have a spring-rest-servlet.xml file which has <mvc:annotation-driven />

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.mp.web.controller.common" />

</beans>

Now if I deploy my WAR on tomcat 8 its running just as expected.

URL --> http://localhost:8080/application-name/rest/media

Now I want to create a Test Case for this REST service. What i did so far

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:spring-rest-servlet.xml")
public class MediaTest {

    MockMvc mockMvc;

    @Before
    public void init(){
        mockMvc = MockMvcBuilders.standaloneSetup(MediaListController.class).build();
    }

    @Test
    public void getAllMediaTest1() throws Exception {
        mockMvc.perform(get("/media")).andExpect(status().isOk());
    }
}

If I run that test I get the following Warning.

WARN  o.s.w.s.PageNotFound - No mapping found for HTTP request with URI [/media] in DispatcherServlet with name ''

I tried that link with other options like

'/' , '/rest/media' , 'http://localhost:8080/app-name/rest/media'

but no use.

I am using Spring 4.0.3

Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49

1 Answers1

9

You need to change your init to this :

 mockMvc = MockMvcBuilders.standaloneSetup(new MediaListController()).build();
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 1
    Hi, Thanks for quick response, I made the changes that you mentioned and now I am experiencing different Exception. `org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getHeader(Ljava/lang/String;)Ljava/lang/String;` – Talha Ahmed Khan Apr 16 '14 at 12:07
  • 1
    Yes, I saw my maven dependencies and it has both 2.5 and 3.0 servlet-api. I removed the 2.5 dependency and its working fine. – Talha Ahmed Khan Apr 16 '14 at 12:36
  • what if there are autowired fields in class MediaListController? It seems Spring won't autowire fields if using new operator? I did what you said but error happened, because the field tagged with @Resource in controller cannot be autowired. – chou Jul 22 '17 at 10:21