2016-09-18 4 views
4

ために401を返すには、ここで 春のテスト私はMVCのために春を使用しています無担保のURL

はここに私のテストクラス

@RunWith(SpringRunner.class) 
@WebMvcTest 
public class ITIndexController { 

    @Autowired 
    WebApplicationContext context; 

    MockMvc mockMvc; 

    @MockBean 
    UserRegistrationApplicationService userRegistrationApplicationService; 

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

    @Test 
    public void should_render_index() throws Exception { 
     mockMvc.perform(get("/")) 
      .andExpect(status().isOk()) 
      .andExpect(view().name("index")) 
      .andExpect(content().string(containsString("Login"))); 
    } 
} 

であるMVCの設定ここで

@Configuration 
@EnableWebMvc 
public class MvcConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("index"); 
     registry.addViewController("/login/form").setViewName("login"); 
    } 
} 

にセキュリティ設定されているテスト

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    @Qualifier("customUserDetailsService") 
    UserDetailsService userDetailsService; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
      .antMatchers("/resources/**", "/signup", "/signup/form", "/").permitAll() 
      .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/login/form").permitAll().loginProcessingUrl("/login").permitAll() 
       .and() 
      .logout().logoutSuccessUrl("/login/form?logout").permitAll() 
       .and() 
      .csrf().disable(); 
    } 

    @Autowired 
    public void configureGlobalFromDatabase(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService); 
    } 
} 
java.lang.AssertionError: Status expected:<200> but was:<401> 
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) 
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81) 
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:664) 
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171) 
at com.marco.nutri.integration.web.controller.ITIndexController.should_render_index(ITIndexController.java:46) 
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) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

が、私はそれが原因URLは春のセキュリティで保護されていますが、私は自分のアプリケーションを実行すると、私もせずにそのURLにアクセスすることができるという事実に失敗したことを理解:私は私のテストを実行すると10は、それがメッセージで失敗します認証されています。

何か間違っていますか?

+0

ブラウザから同一構成の要求の仕事をしていますか? – chrylis

+0

はい、それは動作します –

+0

あなたのテスト構成に問題があるようなにおいがします。 'configure'の中にブレークポイントを置き、テストをデバッグするとどうなりますか? – chrylis

答えて

5

私は答え
春のドキュメントがと言うが見つかりました:

@WebMvcTestは@Controller、@ControllerAdvice、@JsonComponent、 フィルター、WebMvcConfigurerへのSpring MVCインフラストラクチャと リミットスキャン豆を自動設定しますHandlerMethodArgumentResolverです。 Regular この注釈を使用する場合、@Component Beanはスキャンされません。

そしてgithubの中に、この問題に応じて:

https://github.com/spring-projects/spring-boot/issues/5476

@WebMvcTestデフォルト自動構成バネセキュリティによって、ばねのセキュリティテストはクラスパスに存在する場合(私の場合であり、 )。

WebSecurityConfigurerクラスが選択されていないので、既定のセキュリティが自動構成されていたため、セキュリティ設定でセキュリティで保護されていなかった401のURLが表示されました。春のセキュリティデフォルトの自動設定はすべてのURLを基本認証で保護します。私は問題を解決するためにやった

は@ContextConfigurationを持つクラスに注釈を付けることだった、それがマニュアルに記載されて好き@MockBean:

しばしば@WebMvcTestは、単一のコントローラに限定してで使用されます の協力者の模擬実装を提供する@MockBeanとの組み合わせ。ここ

そして

は、MvcConfigとSecurityConfigはすべて私の設定クラスがあるテストクラス

@RunWith(SpringRunner.class) 
@WebMvcTest 
@ContextConfiguration(classes={Application.class, MvcConfig.class, SecurityConfig.class}) 
public class ITIndex { 

    @Autowired 
    WebApplicationContext context; 

    MockMvc mockMvc; 

    @MockBean 
    UserRegistrationApplicationService userRegistrationApplicationService; 

    @MockBean 
    UserDetailsService userDetailsService; 

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

    @Test 
    public void should_render_index() throws Exception { 
     mockMvc.perform(get("/")) 
      .andExpect(status().isOk()) 
      .andExpect(view().name("index")) 
      .andExpect(content().string(containsString("Login"))); 
    } 
} 

アプリケーションです

+3

あなたはおそらく'@ ContextConfiguration'を使用します。 '@Import(SecurityConfig.class)'を単に追加するだけで十分です。 –

+0

詳細については、https://github.com/spring-projects/spring-boot/issues/6514をご覧ください。 –

関連する問題