Java Mockito和Powermockito org.Mockito.exceptions.misusing.InvalidUseOfMatchersException:检测到放错位置的参数匹配器
我想为下一个spring MVC控制器编写单元测试用例Java Mockito和Powermockito org.Mockito.exceptions.misusing.InvalidUseOfMatchersException:检测到放错位置的参数匹配器,java,unit-testing,mockito,powermock,Java,Unit Testing,Mockito,Powermock,我想为下一个spring MVC控制器编写单元测试用例 @Controller @Configuration @PropertySource("classpath:project/web/properties/RealTimeAPI.properties") @RequestMapping("/learnon") public class ClassManagerController { private final Logger logger = Logger.getLogger(Cl
@Controller
@Configuration
@PropertySource("classpath:project/web/properties/RealTimeAPI.properties")
@RequestMapping("/learnon")
public class ClassManagerController {
private final Logger logger = Logger.getLogger(ClassManagerController.class);
@Autowired
private ClassManagerService classManagerService;
@Autowired
private GroupUserService groupUserService;
@RequestMapping(value = "/teacher", method = RequestMethod.GET)
public ModelAndView showClassDetail(HttpServletRequest request, HttpSession httpSession,
@RequestParam(value = "isbn", required = false) String isbn13,
@RequestParam(value = "classId", required = false) Long classId) {
String redirectUrl = "https://example.com/jsp/Login.jsp?reason=failedLogin&redirectUri=https://example.com/secure/Bookshelf";
String accessDeniedUri = "https://example.com/jsp/AccessDenied.jsp";
if (httpSession.getAttribute("USERID") == null) {
return new ModelAndView("redirect:" + redirectUrl);
}
try {
long userId = Long.parseLong(httpSession.getAttribute("USERID").toString());
UserBean user = classManagerService.getUser(userId);
if (httpSession.getAttribute("SCHOOLID") == null) {
httpSession.setAttribute("SCHOOLID", user.getSchoolId());
}
if (httpSession.getAttribute("FULLFILLMENT_YEAR") == null) {
httpSession.setAttribute("FULLFILLMENT_YEAR", user.getFulfillmentYear());
}
String isbn10 = ISBNUtil.convertIsbn13ToIsbn10(isbn13);
String title = "";
ModelAndView mav = null;
ClassManagerBean classBean = null;
if(classId == null && httpSession.getAttribute("classId") != null){
classId = (Long)httpSession.getAttribute("classId");
}
if(classId != null && classId > 0) {
List<UserBean> userBeanList = classManagerService.getUserList(user.getSchoolId(), classId, isbn10);
classBean = classManagerService.getClassById(classId);
classBean.setUserNumber(userBeanList.size());
title = classBean.getTitle();
//Set the view to ClassManager.jsp
mav = new ModelAndView("ClassManager");
mav.addObject("userList", userBeanList);
boolean authorized = userBeanList.stream().anyMatch(u->u.getUserId() == userId);
if(!authorized){
ModelAndView modelAndView = new ModelAndView("redirect:" + accessDeniedUri);
modelAndView.addObject("accessDenied", "true");
return modelAndView;
}
}else{
title = classManagerService.getTitle(isbn10);
//Set the view to createNewClass.jsp
mav = new ModelAndView("CreateNewClass");
classBean = new ClassManagerBean();
classBean.setLo2Flag(true);
classBean.setIsbn(isbn10);
classBean.setTitle(title);
}
httpSession.setAttribute("searchTitle", title);
httpSession.setAttribute("selectedIsbn", isbn10);
httpSession.setAttribute("classId", classId);
mav.addObject("user", user);
mav.addObject("classBean", classBean);
return mav;
} catch (Exception ex) {
ModelAndView mav2 = new ModelAndView("redirect:" + accessDeniedUri);
mav2.addObject("accessDenied", "true");
logger.error("Exception Occurred, Redirecting to Access Denied...", ex);
return mav2;
}
}
}
@控制器
@配置
@PropertySource(“类路径:project/web/properties/RealTimeAPI.properties”)
@请求映射(“/learnon”)
公共类类管理器控制器{
私有最终记录器=Logger.getLogger(ClassManagerController.class);
@自动连线
私有类管理服务类管理服务;
@自动连线
私有组用户服务组用户服务;
@RequestMapping(value=“/teacher”,method=RequestMethod.GET)
公共模型和视图显示类详细信息(HttpServletRequest请求、HttpSession HttpSession、,
@RequestParam(value=“isbn”,required=false)字符串isbn13,
@RequestParam(value=“classId”,required=false)长classId){
字符串重定向URL=”https://example.com/jsp/Login.jsp?reason=failedLogin&redirectUri=https://example.com/secure/Bookshelf";
字符串accessDeniedUri=”https://example.com/jsp/AccessDenied.jsp";
if(httpSession.getAttribute(“USERID”)==null){
返回新的ModelAndView(“重定向:”+redirectUrl);
}
试一试{
long userId=long.parseLong(httpSession.getAttribute(“userId”).toString());
UserBean user=classManagerService.getUser(userId);
if(httpSession.getAttribute(“SCHOOLID”)==null){
setAttribute(“SCHOOLID”,user.getSchoolId());
}
if(httpSession.getAttribute(“完整填写年份”)==null){
setAttribute(“fullfilliment_YEAR”,user.getFulfillmentYear());
}
字符串isbn10=ISBNUtil.convertIsbn13ToIsbn10(isbn13);
字符串标题=”;
ModelAndView mav=null;
ClassManagerBean classBean=null;
if(classId==null&&httpSession.getAttribute(“classId”)!=null){
classId=(长)httpSession.getAttribute(“classId”);
}
如果(classId!=null&&classId>0){
List userBeanList=classManagerService.getUserList(user.getSchoolId(),classId,isbn10);
classBean=classManagerService.getClassById(classId);
setUserNumber(userBeanList.size());
title=classBean.getTitle();
//将视图设置为ClassManager.jsp
mav=新模型和视图(“类管理器”);
addObject(“userList”,userBeanList);
boolean authorized=userBeanList.stream().anyMatch(u->u.getUserId()==userId);
如果(!授权){
ModelAndView ModelAndView=新建ModelAndView(“重定向:+accessDeniedUri”);
addObject(“accessDenied”、“true”);
返回模型和视图;
}
}否则{
title=classManagerService.getTitle(isbn10);
//将视图设置为createNewClass.jsp
mav=新模型和视图(“CreateNewClass”);
classBean=新的ClassManagerBean();
setLo2Flag(true);
setIsbn(isbn10);
setTitle(title);
}
setAttribute(“searchTitle”,title);
setAttribute(“selectedIsbn”,isbn10);
setAttribute(“classId”,classId);
mav.addObject(“用户”,用户);
addObject(“classBean”,classBean);
返回mav;
}捕获(例外情况除外){
ModelAndView mav2=新的ModelAndView(“重定向:+accessDeniedUri”);
mav2.addObject(“accessDenied”、“true”);
错误(“发生异常,重定向到拒绝访问…”,例如);
返回mav2;
}
}
}
我已经为上述类编写了以下单元测试用例
@RunWith(PowerMockRunner.class)
公共类类管理器控制器测试{
public ClassManagerService classManagerService;
public GroupUserService groupUserService;
private MockMvc mockMvc;
@InjectMocks
private ClassManagerController classManagerController;
@Before
public void setUp() {
classManagerService = Mockito.mock(ClassManagerService.class);
groupUserService = Mockito.mock(GroupUserService.class);
mockMvc = MockMvcBuilders.standaloneSetup(classManagerController).build();
}
@Test
public void testShowClassDetail() throws Exception {
HttpServletRequest httpRequest = mock(HttpServletRequest.class);
HttpSession httpSession = mock(HttpSession.class);
Mockito.when(httpSession.getAttribute("USERID")).thenReturn(null);
RequestBuilder request = MockMvcRequestBuilders
.get("/learnon/teacher")
.param("isbn", "1234567890123")
.param("classId", "1")
.accept(MediaType.APPLICATION_JSON);
String modalView = "redirect:" + "https://example.com/jsp/Login.jsp?reason=failedLogin&redirectUri=https://www.example.com/secure/Bookshelf";
ResultActions result = mockMvc.perform(request)
.andExpect(status().is3xxRedirection())
.andExpect(view().name(modalView));
}
@Test
public void testShowClassDetail1() throws Exception {
HttpServletRequest httpRequest = mock(HttpServletRequest.class);
HttpSession httpSession = mock(HttpSession.class);
Mockito.when(httpSession.getAttribute("USERID")).thenReturn(Mockito.anyString());
//Line 87
List<UserBean> spyList = Mockito.mock(List.class);
Mockito.when(classManagerService.getUserList(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString())).thenReturn(spyList);
Mockito.when(spyList.stream().anyMatch(u->u.getUserId() == Mockito.anyLong())).thenReturn(true);
RequestBuilder request = MockMvcRequestBuilders
.get("/learnon/teacher")
.param("isbn", "1234567890123")
.param("classId", "1")
.accept(MediaType.APPLICATION_JSON);
String modalView = "redirect:" + "https://example.com/jsp/AccessDenied.jsp";
ResultActions result = mockMvc.perform(request)
.andExpect(status().is3xxRedirection())
.andExpect(view().name(modalView));
}
公共类管理服务类管理服务;
公共组用户服务组用户服务;
私有MockMvc-MockMvc;
@注射模拟
私有类管理器控制器类管理器控制器;
@以前
公共作废设置(){
classManagerService=Mockito.mock(classManagerService.class);
groupUserService=Mockito.mock(groupUserService.class);
mockMvc=MockMvcBuilders.standaloneSetup(classManagerController.build();
}
@试验
public void testShowClassDetail()引发异常{
HttpServletRequest httpRequest=mock(HttpServletRequest.class);
HttpSession-HttpSession=mock(HttpSession.class);
Mockito.when(httpSession.getAttribute(“USERID”))。然后返回(null);
RequestBuilder request=MockMvcRequestBuilders
.get(“/learnon/teacher”)
.param(“isbn”、“1234567890123”)
.param(“classId”、“1”)
.accept(MediaType.APPLICATION_JSON);
字符串modalView=“重定向:+”https://example.com/jsp/Login.jsp?reason=failedLogin&redirectUri=https://www.example.com/secure/Bookshelf";
ResultActions result=mockMvc.perform(请求)
.andExpect(状态().is3xx重定向())
.andExpect(view().name(modalView));
}
@试验
public void testShowClassDetail1()引发异常{
HttpServletRequest httpRequest=mock(HttpServletRequest.class);
HttpSession-HttpSession=mock(HttpSession.class);
Mockito.when(httpSession.getAttribute(“USERID”))。然后返回(Mockito.anyString());
//第87行
List-spyList=Mockito.mock(List.class);
Mockito.when(classManagerService.getUserList(Mockito.anyLong(),Mockito.anyLong(),Mockito.anyString())。然后返回(spyList);
Mockito.when(spyList.stream().anyMatch(u->u.getUserId()==Mockito.anyLong())。然后返回(true);
RequestBuilder request=MockMvcRequestBuilders
.get(“/learnon/teacher”)
Mockito.when(httpSession.getAttribute("USERID")).thenReturn(Mockito.anyString());
Mockito.when(httpSession.getAttribute("USERID")).thenReturn(new String("anyString"));,