簡體   English   中英

NullPointerException:無法調用 ServletRequestAttributes.getRequest,因為 RequestContextHolder.getRequestAttributes 的返回值為 null

[英]NullPointerException: Cannot invoke ServletRequestAttributes.getRequest because the return value of RequestContextHolder.getRequestAttributes is null

我有一個使用RestTemplate的服務,為此它需要從令牌中獲取信息。

public RoleKeycloak getRoleId(String idRole, String projectId) {

    ResponseEntity<String> response = restTemplate.postForEntity(URL, new HttpEntity<>(query, getHeaders()), String.class);
   //more operations 
}

我通過 getHeaders() 方法獲取 Headers 信息,該方法使用FeignClientInterceptor ,負責從 header 獲取授權。

private HttpHeaders getHeaders() {
    headers.set(ConstantsUtils.Authorization, FeignClientInterceptor.getBearerTokenHeader());
    headers.set(ConstantsUtils.Content_Type, ConstantsUtils.GraphQL);
    return headers;
}


@Component
public class FeignClientInterceptor implements RequestInterceptor {

      public static final String getBearerTokenHeader() {
          return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader(AUTHORIZATION_HEADER);
      }
}


    
@Test
void createRoleTest() throws ParseException, IOException {
String response = "createRoleProject";
    ResponseEntity<String> responseEntity = new ResponseEntity<String>(response, HttpStatus.ACCEPTED);
    
    given(restTemplate.postForEntity("myurl", getHeaders(), String.class))
        .willReturn(responseEntity);
    RoleKeycloak roleKeycloak = roleServiceKeycloak.createRole("admin", "idProject");
    assertNotNull(roleKeycloak);
}

我正在嘗試使用jUnit測試此方法,但我不能,即使使用互聯網上的信息也是如此。 我試圖模擬FeignClientInterceptor但我不能,總是在NullPointerException ();

根據您的評論和問題,我建議以下解決方案:

在這里,我使用模擬 static 和MockHttpServletRequest在測試用例中,您可以創建一個 MockHttpServletRequest 並設置一個 header。 稍后,使用 mockStatic,您可以啟用存根以執行 RequestContextHolder.getRequestAttributes RequestContextHolder.getRequestAttributes()

@Test
    void test() {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader(HttpHeaders.AUTHORIZATION, "SomeheaderValue");

        ServletRequestAttributes attributes = new ServletRequestAttributes(request);
        RequestContextHolder.setRequestAttributes(attributes);

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        MockedStatic<RequestContextHolder> mockedStatic = mockStatic(RequestContextHolder.class);
        mockedStatic.when(() -> RequestContextHolder.getRequestAttributes()).thenReturn(requestAttributes);

        System.out.println(FeignClientInterceptor.getBearerTokenHeader()); // this will print "SomeheaderValue"
    }

您需要使用以下依賴項來支持模擬 static:

         <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>

然后,您可以在單元測試中直接從getHeaders方法調用FeignClientInterceptor.getBearerTokenHeader()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM