2017-10-25 6 views
0

私は春MockMVCを使用して春休みコントローラをテストしています。 私は模擬HTTPリクエストを実行して、応答を確認することができます。 マイセットアップがWebApplicationContext autowires: MockMVCと@ComponentScanを使用してspringSecurityFilterChain Beanが見つかりません

@RunWith(SpringRunner.class) 
@WebAppConfiguration 
@ContextConfiguration 
public class MyTestClass { 

@Autowired 
private WebApplicationContext webAppContext; 

private MockMvc mockMvc; 

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

は何のコントローラを初期化し、私は内部のconfigクラスを持っているコンテキストに追加するMockMVCを伝えるために:

@Configuration 
@ComponentScan(
     basePackages = {"com.acme"}, 
     useDefaultFilters = false, 
     includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value={MyRestController.class, MyRestControllerDependency.class}) 
     }) 
@EnableWebMvc 
static class MyTestClassConfig implements WebMvcConfigurer { 

    @Bean 
    public MyClassRepository myClassRepository() { 
     return Mockito.mock(MyClassRepository.class); 
    } 

をし、これが魔法のように動作します。 MockMVCは、MyRestControllerをロードしたコンテキストでコンテナを回転させ、MyRestControllerによってマップされたURLを提供します。 今、いくつかのMyRestControllersメソッドには@PreAuthorize("hasAuthority('ADMINISTRATOR')")という注釈が付けられていますので、次のステップはそれをテストすることです。春のセキュリティテストガイド spring security and test-mockmvc の統合部分を読む

私が何をすべきすべてはMockMvcBuilders .apply()の関数呼び出しでorg.springframework.security.test.web.servlet.setup.springSecurity()を呼び出しているようです。これは動作しません

、私が取得:

java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used. 

私はそうのように私の内部のconfigクラスにBean定義を追加してみました:

Filter springSecurityFilterChain() { 
     return new FilterChainProxy(); 
    } 

これはspringSecurityFilterChainを提供していますが、インスタンスであります空とするとき、私はNPEを取得し、テスト実行します。

understandaで、
java.lang.NullPointerException 
at org.springframework.security.web.FilterChainProxy.getFilters(FilterChainProxy.java:224) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 

をつまり、FilterChainProxyを直接インスタンス化することによって、実際には空です。

私は、SpringのFilterChainProxyインスタンスを含むようにコンポーネントスキャンを構成する必要があると思われます。 問題がある場合は、私はどのクラスを含めるべきか分かりません。私はすべてのクラスを含む試してみました:

 includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value={TenantAPI.class, TenantManager.class}), 
          @ComponentScan.Filter(type = FilterType.REGEX, pattern="^org\\.springframework\\.security.*")}) 

しかし、これは、同じエラー与える:「名前springSecurityで豆を確認する..」私は、追加の正規表現フィルタなしでテストを実行するときのように。

誰かが私が内部クラスを使っているときにspringSecurityFilterChainを取得する方法を知っていますか?MockMVCをインスタンス化させますか?

答えて

1

@Configurationクラスは、どこかでWebSecurityConfigurerAdapterに拡張する必要がありますか?これは、Springセキュリティを設定するために必要な設定です。

テスト設定でコンポーネントスキャンにこのクラスを含める必要があります。

+0

はいあります。私は実際にそれをしましたが、それはたくさんのものをautowiresので逃げた。春は、 "_somename_"という名前の豆を作ることができないと訴えました...今日はもっとうまく行くでしょう、ありがとう! – Jarvis

関連する問題