代码之家  ›  专栏  ›  技术社区  ›  Klimiuk S

在测试上下文中未重写Spring@Configuration

  •  0
  • Klimiuk S  · 技术社区  · 7 年前

    今天我将我的项目从Spring Boot 1.5.9更新到了2.1.1,我的一些测试停止了。当我开始测试时,控制台上会弹出错误:

    问题是我在SecurityConfig类中定义了这种类型的bean,但我在TestApplication类的测试包中重写了这种配置。安全配置在这里定义为静态内部类。我尝试过不同的方法,包括Spring概要文件和@Primary注释,但似乎没有任何效果,Spring也不像以前那样选择我的测试配置。唯一有效的是,我删除了SecurityConfig类的非测试版本,测试版本成为这种类型的唯一bean。

    有人能告诉我如何覆盖这个原始配置,或者如何关闭Spring安全性以进行测试吗?或者也许有一种方法可以迫使Spring不选择那个非测试@Configuration bean?

        @Configuration
        @EnableWebSecurity
        public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
            @Autowired
            AuthenticationEntryPoint authEntryPoint;
    
            @Autowired
            BasicAuthenticationProvider basicAuthProvider;
    
            @Autowired
            PreAuthenticatedUserDetailsService preAuthUserDetailsService;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .authorizeRequests()
                        .antMatchers("/rest/query/id/*/user/*",
                                "/rest/files/**/*").hasAnyRole("CLIENT", "SYSTEM")
                        .antMatchers("/public/api/management/**/*").hasRole("SYSTEM")
                        .antMatchers("/public/api/**/*").hasAnyRole("SYSTEM", "USER")
                        .antMatchers("/rest/**/*").hasRole("SYSTEM")
                        .and()
                    .x509()
                        .userDetailsService(preAuthUserDetailsService)
                        .and()
                    .httpBasic()
                        .authenticationEntryPoint(authEntryPoint)
                        .and()
                    .sessionManagement()
                        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                        .and().csrf().disable();
            }
    
            @Autowired
            public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {       
                auth.authenticationProvider(basicAuthProvider);     
            }
    
            @Override
            public void configure(WebSecurity web) throws Exception {
                web.ignoring().antMatchers("/").antMatchers("/rest/files/name/**");
            }
        }
    

    @SpringBoot应用程序

    @Configuration
    @EnableWebSecurity
    public static class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/**").permitAll()
            .and().csrf().disable();
        }
    }
    

    }

    套件中的示例测试

    @RunWith(SpringRunner.class)
    @WebMvcTest(DocumentManagementController.class)
    public class DocumentManagementControllerTests {
    
        @Autowired
        MockMvc mvc;
    
        @MockBean
        SystemMetadataService systemMetadataService;
    
        @MockBean
        CustomMetadataService customMetadataService;
    
        @MockBean
        PrinterService printerService;
    
        @MockBean
        EventLoggerService eventLoggerService;
    
        @Captor ArgumentCaptor<String> systemCaptor;
        @Captor ArgumentCaptor<String> clientCaptor;
        @Captor ArgumentCaptor<Boolean> holdCaptor;
        @Captor ArgumentCaptor<String> retentionCaptor;
        @Captor ArgumentCaptor<String> objectPathCaptor;
        @Captor ArgumentCaptor<Boolean> accessCaptor;
        @Captor ArgumentCaptor<Boolean> manualProcessingCaptor;
        @Captor ArgumentCaptor<Boolean> incorrectCaptor;
        @Captor ArgumentCaptor<Integer> statusCaptor;
        @Captor ArgumentCaptor<Boolean> noTemplateCaptor;
    
        @Test
        public void setDocumentAccess_givenProperData_shouldReturnOk() throws Exception {
            when(customMetadataService.setDocumentAccess(anyString(), anyBoolean()))
            .then(inv -> new HcpCreateObjectResult(inv.getArgument(0)));
    
            Boolean accessForbidden = true; String objectPath = "path";
    
            mvc.perform(get("/rest/management/access/forbid/"+accessForbidden+"?objectPath="+objectPath))
            .andExpect(status().isOk());
    
            verify(customMetadataService).setDocumentAccess(objectPathCaptor.capture(), accessCaptor.capture());
            assertThat(objectPathCaptor.getValue(), is(equalTo(objectPath)));
            assertThat(accessCaptor.getValue(), is(equalTo(accessForbidden)));
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Klimiuk S    7 年前

    我设法用计算机来完成这项工作 @Profile @ActiveProfiles . 但我必须提取我的静态内心 @Configuration 类转换为另一个java文件,然后它自动开始工作。仍然没有发现为什么它能在早期版本的Spring Boot中工作