2016-08-03 8 views
1

この質問は他の場所では見つかりませんでした。CsrfでSpringセキュリティユニットテストを実行するとHttpServletResponseが見つからない

次のように私は私のユニットテストのセットアップを持っている:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
//@DatabaseSetup("classpath:decks.xml") 
@WebIntegrationTest 
public class DeckControllerTest { 
    @Autowired 
    private WebApplicationContext context; 

    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     mockMvc = MockMvcBuilders 
       .webAppContextSetup(context) 
       .apply(springSecurity()) 
       .build(); 
    } 

    @Test 
    @WithMockUser 
    public void addDeck_ShouldRedirectIfUserLoggedInAndPostsInvalidDeck() throws Exception { 
     DeckbuilderForm form = new DeckbuilderForm(); 
     ObjectMapper mapper = new ObjectMapper(); 
     String queryString = mapper.convertValue(form, UriFormat.class).toString(); 
     mockMvc.perform(post("/decks") 
      .with(csrf()) 
      .contentType(MediaType.ALL) 
      .content(queryString) 
     ) 
       .andExpect(status().is3xxRedirection()) 
       .andExpect(flash().attributeExists("flash")); 
    } 

    @Test 
    @WithMockUser 
    public void addDeck_ShouldBeOkWithValidInput() throws Exception { 
     DeckbuilderForm form = sampleValidDeckbuilderForm(); 
     ObjectMapper mapper = new ObjectMapper(); 
     String queryString = mapper.convertValue(form, UriFormat.class).toString(); 

     mockMvc.perform(post("/decks") 
      .with(csrf().useInvalidToken()) 
      .content(queryString) 
     ) 
       .andExpect(status().is3xxRedirection()); 
    } 
} 

私がテストを実行すると、次のスタックトレースを受け取る:

java.lang.IllegalArgumentExceptionが:HttpServletRequestの属性 は、HttpServletResponseのが含まれている必要があります属性の場合 javax.servlet.http.HttpServletResponse

これは、クラスLazyCsrfTokenRepositoryから来ている:

private HttpServletResponse getResponse(HttpServletRequest request) { 
    HttpServletResponse response = (HttpServletResponse) request 
      .getAttribute(HTTP_RESPONSE_ATTR); 
    if (response == null) { 
     throw new IllegalArgumentException(
       "The HttpServletRequest attribute must contain an HttpServletResponse for the attribute " 
         + HTTP_RESPONSE_ATTR); 
    } 
    return response; 
} 

この問題を解決するための任意の提案が高く評価されるだろう、私は通常、1の代わりに@SpringApplicationConfigurationの@ContextConfigurationを使用するであろうことを理解しますが、私は私だけので、この方法を使用することができていますJava Configを使用していて、テスト用に構成を複製したくない場合

答えて

3

spring-boot 1.4にアップグレードした後も同じ問題が発生しました。問題は、何らかの理由で私のpomに特定のバージョンのspring-security-test(4.0.2)が含まれていることでした。この特定のバージョンを削除すると、プロジェクトでバージョン4.1.1が使用され、問題が解決されました。バージョン4.1.0(https://github.com/spring-projects/spring-security/compare/4.1.0.RELEASE...master)のリリースコミットを見ると、spring-boot 1.4へのアップデートが含まれているので意味があります。

+0

最近、いくつかの主要なリファクタリングが行われました。テストが更新されるまで数日かかるかもしれません。私はそれがどのように進むのかを知らせます。私は現在、私の春のセキュリティテストバージョンを4.1.1から4.1.0に変更しました。 (4.1.1でエラーが発生しました) –

+0

私の主要なリファクタリングの後、新しい@WebMvcTestアノテーションを使用した後で修正されているようですが、なぜ4.1.0が私の問題を解決したのか不思議です。問題の –

+0

私はspring-security-test 4.0.4と同じ問題がありました。 4.1.4または4.2.1へのアップグレードはそれを修正しました。 (スプリングブート1.4.3の場合) –

関連する問題