我正在为使用refreshAndGetAuthenticationToken
(HttpServletRequest
请求)之类的方法的java应用程序编写测试用例。所以,我尝试为这个方法创建测试用例,但由于HttpServletRequest
是一个接口,我无法为它创建一个对象。我也试过mockito
,但是不支持以json格式传递请求。
测试案例是
@Test
public final void testRefreshAndGetAuthenticationToken() {
Gson gson = new GsonBuilder().serializeNulls().create();
String token = given().contentType("application/json").body(authenticationRequest).when().post("/auth").
then().statusCode(200).extract().path("token");
// MockHttpServletRequest request = new MockHttpServletRequest();
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
mockRequest.setAttribute("Authorization", token);
Mockito.when(mockRequest.getHeader(tokenHeader)).thenReturn(token);
given().contentType("application/json").header("request",mockRequest).when().get("/refresh").then().statusCode(200);
}