【发布时间】:2017-11-03 02:10:32
【问题描述】:
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>()));
}
}
}
出于测试目的,
当我们设置 id=null 时,如何获取 ServerResponse 对象内的 errorCode 集。
@RunWith(MockitoJUnitRunner.class)
class Test {
@InjectMocks
private Permission permission;
@Mock
private HttpServletRequest servletRequest;
@Mock
private ContainerRequestContext containerRequestContext;
@Test
public void test()
{
when(servletRequest.getPathInfo()).thenReturn("/getid");
when(containerRequestContext.getId()).thenReturn(null);
permission.filter(containerRequestContext);
Response r = //Need the code. How to get the Response that i have set on permission class when id=null so that i can fetch the errorCode from that.
//With that erroCode i can write my test methods.
}
}
【问题讨论】:
-
能否请您附上您当前的测试尝试。答案取决于您的测试设置。这是an option。
-
添加了测试类和描述。
标签: java spring-mvc junit mockito