[英]Spring @Configuration not overriden in test context
Today I updated my project from Spring Boot 1.5.9 to 2.1.1, and some of my tests stopped working. 今天,我将项目从Spring Boot 1.5.9更新到2.1.1,并且一些测试停止了工作。 When i start the tests, error pops on console:
当我开始测试时,错误在控制台上弹出:
Field authEntryPoint in com.example.rest.config.SecurityConfig required a bean of type 'com.example.rest.service.auth.entrypoints.AuthenticationEntryPoint' that could not be found.
com.example.rest.config.SecurityConfig中的字段authEntryPoint需要找不到类型为'com.example.rest.service.auth.entrypoints.AuthenticationEntryPoint'的bean。
The problem is I have bean of this type defined in my SecurityConfig class, but I am overriding this configuration in my test package in TestApplication class. 问题是我在SecurityConfig类中定义了这种类型的bean,但是我在TestApplication类的测试包中覆盖了此配置。 Security config is defined there as static inner class.
安全配置在那里定义为静态内部类。 I have tried different approaches including Spring profiles and @Primary annotation, but nothing seems to work and Spring doesn't pick my test configuration like it did before.
我尝试了不同的方法,包括Spring概要文件和@Primary批注,但是似乎没有任何效果,并且Spring没有像以前那样选择我的测试配置。 Only thing that worked was when I deleted the non-test version of SecurityConfig class and test version became only bean of this type.
唯一起作用的是当我删除SecurityConfig类的非测试版本,并且测试版本仅成为这种类型的bean时。
Can someone tell me how do I override this original configuration or how to turn off Spring Security just for testing? 有人可以告诉我如何覆盖此原始配置,或者如何仅出于测试目的而关闭Spring Security? Or maybe there is a way to force Spring not to pick up that non-test @Configuration bean?
或者也许有一种方法可以迫使Spring不要拿起未经测试的@Configuration bean?
SecurityConfig.class SecurityConfig.class
@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/**");
}
}
Test SpringBootClass with SecurityConfig inside 使用SecurityConfig在内部测试SpringBootClass
@SpringBootApplication public class TestApplication { @SpringBootApplication公共类TestApplication {
@Configuration
@EnableWebSecurity
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll()
.and().csrf().disable();
}
}
} }
Example test from the suite 套件中的示例测试
@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)));
}
I managed to do make this work using @Profile
and @ActiveProfiles
. 我设法使用
@Profile
和@ActiveProfiles
来完成这项工作。 But i had to extract my static inner @Configuration
class to another java file and then it automagically started to work. 但是我必须将我的静态内部
@Configuration
类提取到另一个Java文件,然后它自动开始工作。 Still haven't found why it worked in earlier version of Spring Boot 仍然没有找到它为什么在早期版本的Spring Boot中起作用的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.