无法测试有效的Spring Boot配置文件

时间:2018-08-05 10:56:38

标签: spring-boot integration-testing profiles

我有spring boot应用程序。我想为SecurityConfig创建两个配置文件:dev,prod。第一次尝试是从WebSecurityConfigurerAdapter扩展了两个类,但后来我在SecurityConfig类中创建了两个Bean。 我的配置如下:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Bean
    public JwtAuthorizeFilter jwtAuthenticationFilter() throws Exception {
        return new JwtAuthorizeFilter();
    }

    @Bean
    @Profile("prod")
    public WebSecurityConfigurerAdapter prod() {
        return new WebSecurityConfigurerAdapter() {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                //some code...
            }
        };
    }

    @Bean
    @Profile("dev")
    public WebSecurityConfigurerAdapter dev() {
        return new WebSecurityConfigurerAdapter() {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                        //some code...
            }
        };
    }

    @Bean
    public WebMvcConfigurer corsConfig() {
        return new WebMvcConfigurerAdapter() {
                       //some code...
        };
    }
}

用于运行应用程序的类:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.setProperty("spring.profiles.active", "dev");
        SpringApplication.run(Application.class, args);
    }
}

启动应用程序时,如何测试正确的配置文件是否处于活动状态? 当我添加空测试

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = SecurityConfig.class, loader=AnnotationConfigContextLoader.class)
public class SpringProfileConfigurationTest {


    @Autowired
    public WebSecurityConfigurerAdapter securityConfig;

    @Test
    public void testSecurityConfigDevActiveProfile() {

    }

}

它因错误而掉落

 .NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework
      .security.config.annotation.web.configuration.WebSecurityConfigurerAdapter'
      available: expected at least 1 bean which qualifies as autowire candidate.
      Dependency annotations:{@org.springframework.beans.factory.annotation
      .Autowired(required=true)}

UPD :将“类”添加到@ContextConfiguration,然后出现另一个错误:

  

BeanCreationException:创建在org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration中定义的名称为'springSecurityFilterChain'的bean时出错:通过工厂方法的Bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法'springSecurityFilterChain'抛出了异常;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'jwtAuthenticationFilter'的bean时出错:通过字段'userDetailsS​​ervice'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“ org.springframework.security.core.userdetails.UserDetailsS​​ervice”的合格Bean:应该至少有1个有资格作为自动装配候选的Bean。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

因此,现在的问题出在org.springframework.security.core.userdetails.UserDetailsS​​ervice中-无法自动装配。在

中使用
@Component
public class JwtAuthorizeFilter extends OncePerRequestFilter {

    @Autowired
    private UserDetailsService userDetailsService;
    //some code;
}

1 个答案:

答案 0 :(得分:0)

为了将Security Bean添加到测试中,请在ContextConfiguration定义中添加SecurityConfig

@ContextConfiguration(classes = {SecurityConfig.class})