Struts2 如何在java junit中将userPrincipal()设置为null

Struts2 如何在java junit中将userPrincipal()设置为null,struts2,junit4,spring-3,Struts2,Junit4,Spring 3,我想从应用程序的junit中将UserPrincipal()设置为null。为此,我尝试将请求设置为null,但得到了null指针异常。请帮我解决这个问题。 下面是我的代码 if(request.getUserPrincipal()!=null){ username=request.getUserPrincipal().getName(); } 我想使条件为false。我已找到解决方案 从junit获取MockHttpServletRequest并将userprinciple设置为n

我想从应用程序的junit中将UserPrincipal()设置为null。为此,我尝试将请求设置为null,但得到了null指针异常。请帮我解决这个问题。 下面是我的代码

if(request.getUserPrincipal()!=null){
     username=request.getUserPrincipal().getName();
}

我想使条件为false。

我已找到解决方案

从junit获取MockHttpServletRequest并将userprinciple设置为null

MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
httpServletRequest.setUserPrincipal(null);

我已经找到了解决办法

从junit获取MockHttpServletRequest并将userprinciple设置为null

MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
httpServletRequest.setUserPrincipal(null);

如何
request.setUserPrincipal(null)
.request.setUserPrincipal(null)怎么样