4

RestController のテスト ケースを作成しようとしています。ここに単純な RestContorller があります

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

そして、私は持っているspring-rest-servlet.xmlファイルを持っています<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>

WAR を tomcat 8 にデプロイすると、期待どおりに実行されます。

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

次に、この REST サービスのテスト ケースを作成します。私がこれまでにやったこと

@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());
    }
}

そのテストを実行すると、次の警告が表示されます。

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

次のような他のオプションでそのリンクを試しました

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

しかし、役に立たない。

私は使っているSpring 4.0.3

4

1 に答える 1