问题描述
下面是我的代码,用于使用Mockito对控制器类进行单元测试.
Below is my code to unit test my controller class using mockito.
但是我遇到如下运行时错误
but i am getting run time errors as below
java.lang.NoClassDefFoundError:org/springframework/web/servlet/FlashMapManager 在org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup(MockMvcBuilders.java:71)
java.lang.NoClassDefFoundError: org/springframework/web/servlet/FlashMapManager at 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"));
}
}
推荐答案
您需要静态导入get
:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
您还使用了status
和view
.您还需要导入这些内容:
You're also using status
and view
. You need to import those too:
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
要在编辑中回答新问题,请确保您在类路径中具有最新的servlet API.如果您使用的是Maven,请添加以下内容:
to answer the new question in your edit, make sure you have the latest servlet API in your class path. If you're using Maven, add this:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
这篇关于Mockito与Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!