1
@Controller
public class MyController {    

    @RequestMapping(method = RequestMethod.GET, value = "/testParam")
    public String update() {
        return "test";
    }
}

私のテストファイル:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:/META-INF/config/applicationContext.xml"})
public class MyControllerTest {
    private MockMvc mockMvc;
    @Autowired
    private WebApplicationContext wac;

       @Test
       public void testUpdate2() throws Exception {
             this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
                    mockMvc.perform(get("/testParam")).andDo(print())
                    .andExpect(status().isOk())
                    .andExpect(forwardedUrl("test"));
       }


        @Test
        public void testUpdate() throws Exception {
            MockMvcBuilders.standaloneSetup(new MyController()).build()
                    .perform(get("/testParam")).andDo(print())
                    .andExpect(status().isOk())
                    .andExpect(forwardedUrl("test"));
        }
}

質問 1): 上記のテストを実行すると、testUpdate2 が AssertionError: Status expected <200> but was:<404> と言って失敗します

2) RequestMethod.POST の場合、どうすればテストできますか?

例えば:

@RequestMapping(method = RequestMethod.POST, value = "/insert")
    public String insertRequests() {
        return "page";
    }

テスト:

@Test
public void insert() throws Exception {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
            mockMvc.perform(post("/insert")).andDo(print())
            .andExpect(status().isOk())
            .andExpect(forwardedUrl("page"));
}

上記のテストは失敗します: AssertionError: Status expected <200> but was:<404>

編集

私の jsps ファイルは次の場所にあります: "\src\main\webapp\WEB-INF\jsps\pages\test.jsp" 私は tiles.xml ファイル構成を使用しています。

xml ファイル:

 <beans..>
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsps/pages/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
4

1 に答える 1

1

ビュー リゾルバーで、プレフィックス値を "/WEB-INF/" にマップしましたか。もしそうなら、あなたの

                .andExpect(forwardedUrl("test"));

する必要があります

              .andExpect(forwardedUrl("/WEB-INF/test.jsp"));
于 2013-07-13T00:27:00.563 に答える