spring unit testing controller不解析WebAppContext

时间:2016-02-11 11:46:40

标签: spring

我已经阅读了关于单元测试spring MVC的在线教程,我已从教程中复制但WebAppContext.class从未解析为类型。这应该解决什么?

package com.doyleisgod.springmavenexample.controllers;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.Arrays;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebAppContext.class})
@WebAppConfiguration
public class HomeControllerTest {
       private MockMvc mockMvc;
       @Test
        public void indexTest() {

            mockMvc.perform(get("/index.htm", 1L))
                    .andExpect(status().isOk())
                    .andExpect(view().name("index"))
                    .andExpect(forwardedUrl("/WEB-INF/views/index.jsp"));

        }
}

Eclipse显示WebAppContext cannot be resolved to a type

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你是指 org.springframework.web.context.WebApplicationContext;要么 org.springframework.test.context.web.WebAppConfiguration;

您需要Spring-test依赖项才能解决这些问题。