0
以下是我使用mockito單元測試我的控制器類的代碼。Mockito彈簧mvc控制器測試
但我得到如下
java.lang.NoClassDefFoundError運行時錯誤:組織/ springframework的/網絡/的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"));
}
}
您好Marcin,我需要單元測試我的控制器,所以我使用mockito與mockmvc來呼叫控制器...但結束了那個例外...對此有什麼想法? – 2014-11-15 15:30:48
你在混合2件事。要單元測試你的控制器,你不需要MockMvc。你爲什麼要使用它? 只需在您的測試和單元測試中創建您的控制器實例即可。 MockMvc將用於集成測試。 – 2014-11-16 16:32:37