我的@WebMvcTest 测试类如何在 Spring Boot 中使用此 JDBC 身份验证配置进行初始化? [英] How can my @WebMvcTest test classes initialize with this JDBC authentication configuration in Spring Boot?

查看:25
本文介绍了我的@WebMvcTest 测试类如何在 Spring Boot 中使用此 JDBC 身份验证配置进行初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 Spring Boot 应用程序配置为使用本地数据库进行身份验证,并且可以正常工作(有一个警告,参见 我的另一个问题),但并非我的所有测试类都适用于新配置.

I have configured my Spring Boot application to use a local database for authentication, and it works (with one caveat, c.f. my other question), but not all of my test classes work well with the new configuration.

这是配置的相关部分(查看全部这里):

Here's the relevant part of the configuration (see it all here):

@Autowired
private DataSource dataSource;

@Override
public void configure(AuthenticationManagerBuilder builder) throws Exception {
    builder .jdbcAuthentication()
            .dataSource(dataSource)
            .withUser(User.withUsername("admin").password(passwordEncoder().encode("pass")).roles("SUPER"));
    logger.debug("Configured app to use JDBC authentication with default database.");
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

使用 @SpringBootTest@AutoConfigureMockMvc 修饰的测试类工作(例如,这个).据我了解,这些自动配置应用程序的各种 Bean 并将它们一起测试(一种集成测试形式).

The test classes decorated with @SpringBootTest and @AutoConfigureMockMvc work (for example, this one). As I understand it, these ones auto-configure the various Beans of the application and test them together (a form of integration test).

我在使用 @WebMvcTest 修饰的测试类(例如 这个).这些应该只测试一个 Controller 类本身,使用各种 Bean 和其他依赖项的模拟对象.

I'm having trouble with the test classes decorated with @WebMvcTest (such as this one). These are supposed to test just one Controller class itself, using mock objects for the various Beans and other dependencies.

  • 自从实施上述配置后,他们首先开始崩溃,出现 UnsatisfiedDependencyException ...没有可用的类型为 'javax.sql.DataSource' 的合格 bean".
  • 然后我将这些行添加到每个这样的测试用例中:
  • Since implementing the configuration above, they first started crashing with an UnsatisfiedDependencyException ... "No qualifying bean of type 'javax.sql.DataSource' available".
  • I then added these lines to each such test case:
@MockBean
private DataSource dataSource;

  • 第一个异常似乎消失了,但现在我的日志中出现以下错误:
    在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/security/config/annotation/web/configuration中定义名称为'springSecurityFilterChain'的bean创建错误/WebSecurityConfiguration.class]:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [javax.servlet.Filter]:工厂方法springSecurityFilterChain"抛出异常;嵌套异常是 org.springframework.jdbc.CannotGetJdbcConnectionException:无法获得 JDBC 连接:DataSource 从 getConnection() 返回 null:javax.sql.DataSource#0 bean
  • 这些测试在一些地方使用了 @WithMockUser 注释,预计不会使用真正的数据库或 JDBC 连接,因为它们每个都只是测试单个控制器.

    These tests use the @WithMockUser annotation in a few places and are not expected to use the real database or the JDBC connection, since they're each just testing a single controller.

    我的问题:如何在不使测试类失败的情况下将 @WebMvcTest 与我当前的安全配置结合使用?是否有一个方便的 Spring Boot 注释我应该添加到测试类中?是不是我的安全配置错了?

    My question: How can I use @WebMvcTest with my current security configuration without the test classes failing? Is there a convenient Spring Boot annotation I should add to the test class? Am I doing the security configuration wrong?

    推荐答案

    使测试正常工作的解决方案是将此属性添加到我的 application.yaml 中:

    A solution that made the tests work was to add this property to my application.yaml:

    spring:
      datasource: 
        initialization-mode: always
    

    或者,如果您更喜欢使用 application.properties 配置文件,它看起来像这样:

    Or if you prefer to use an application.properties config file, it would look like this:

    spring.datasource.initialization-mode=always
    

    我感谢用户 Japan Trivedi,他在我的相关问题中指出了这一点.

    I give credit to user Japan Trivedi who pointed me to this on my related question.

    这篇关于我的@WebMvcTest 测试类如何在 Spring Boot 中使用此 JDBC 身份验证配置进行初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆