与Mockito的春季mvc控制器测试

时间:2014-11-14 15:55:21

标签: spring-mvc junit mockito

下面是我使用mockito对我的控制器类进行单元测试的代码。

但是我的运行时错误如下所示

java.lang.NoClassDefFoundError:org / springframework / web / servlet / FlashMapManager     在org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup(MockMvcBuilders.java:71)

任何人都可以帮助我吗

 package com.emc.elms.eomp.controller.ela;

    import java.util.ArrayList;
    import java.util.List;

    import org.apache.log4j.Logger;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.MockitoAnnotations;
    import org.mockito.runners.MockitoJUnitRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.util.Assert;

    import com.emc.elms.eomp.constants.EOMPConstants;
    import com.emc.elms.eomp.controller.ELAController;
    import com.emc.elms.eomp.manager.ela.ELAManager;
    import com.emc.elms.eomp.vo.ela.SalesOrderVO;

    @RunWith(MockitoJUnitRunner.class)
    //@ContextConfiguration({"classpath*:junit_servlet-context.xml"})
    public class ELAControllerTest {

        @Mock
        ELAManager elaManager;



        @InjectMocks
        private  ELAController elaController;

        @Mock
        List<SalesOrderVO> salesOrderVOs;

        private MockMvc mockMvc;

        Logger logger = Logger.getLogger(ELAControllerTest.class);

        @Before
        public  void setUp() throws Exception {

            MockitoAnnotations.initMocks(this);
             mockMvc = MockMvcBuilders.standaloneSetup(elaController).build();

        }

        @Test
        public void testHome() {
            String view = elaController.home();
            Assert.isTrue(view.equals("home"));
        }

        @Test
        public void testGetELAList() {

             salesOrderVOs = new ArrayList<SalesOrderVO>();

            doReturn(salesOrderVOs).when(elaManager).getELAList(EOMPConstants.STR_BLANK,EOMPConstants.STR_BLANK,null);

            // when(elaManager.getELAList(EOMPConstants.STR_BLANK,EOMPConstants.STR_BLANK,null)).thenReturn(salesOrderVOs);

             //when(elaManager.getELAList(EOMPConstants.STR_BLANK,EOMPConstants.STR_BLANK,any(Date.class))).thenReturn(salesOrderVOs);

              mockMvc.perform(get("/ela/elaVslaReport")
                        .param(EOMPConstants.STR_SORT_COLUMN, "salesOrderNumber")
                        .param("reportType", "Active"))
                        .andExpect(status().isOk())
                        .andExpect(view().name("elaVslaReport"));

              mockMvc.perform(get("/ela/elaVslaReport")
                        .param(EOMPConstants.STR_SORT_COLUMN, "salesOrderNumber")
                        .param("reportType", "All"))
                        .andExpect(status().isOk())
                        .andExpect(view().name("elaVslaReport"));

              mockMvc.perform(get("/ela/elaVslaReport")
                        .param(EOMPConstants.STR_SORT_COLUMN, "lac")
                        .param("reportType", "Active"))
                        .andExpect(status().isOk())
                        .andExpect(view().name("elaVslaReport"));
        }

    }

1 个答案:

答案 0 :(得分:0)

你真的想在这里测试什么?您正在将集成测试与单元测试混合。

如果你想集成测试那个控制器 - 用模拟mvc做。 如果你想进行集成测试 - 使用Mockito(如果有必要对该类进行单元测试)。