Class Permission implements ContainerRequestContext
{
@context
HttpServletRequest servletRequest;
public void filter(ContainerRequestContext containerRequestContext) throws IOException
{
String Path=servletRequest.getPathInfo();
int id=containerRequestContext.getId();
if(id==null){
containerRequestContext.abortWith(new ServerResponse(ExceptionUtil.getErrorMessageObject(...),errorCode,new Header<Object>()));
}
}
}
如何为过滤方法编写Test。我能够模拟ContainerReqeustContext。但是如何模拟HttpServletRequest,因为它不是filter()
方法的输入。
答案 0 :(得分:0)
您可以使用@InjectMocks
注释:
@RunWith(MockitoJUnitRunner.class)
class Test {
@InjectMocks
private Permission permission;
@Mock
private HttpServletRequest servletRequest;
@Test
public void test()
{
permission.filter(...);
}
}
或者您可以使用简单的设置器。
Class Permission implements ContainerRequestContext
{
@context
HttpServletRequest servletRequest;
public void filter(ContainerRequestContext containerRequestContext) throws IOException
{
...
}
void setHttpServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
{
}
@RunWith(MockitoJUnitRunner.class)
class Test {
private Permission permission = new Permission();
@Mock
private HttpServletRequest servletRequest;
@Test
public void test()
{
permission.setHttpServletRequest(servletRequest);
permission.filter(...);
}
}