Java Spring启动测试-没有符合条件的Bean异常
在尝试使用springboot框架进行一些测试时,我遇到了一个问题,即查找测试单元所依赖的BeanJava Spring启动测试-没有符合条件的Bean异常,java,spring,spring-boot,spring-test,Java,Spring,Spring Boot,Spring Test,在尝试使用springboot框架进行一些测试时,我遇到了一个问题,即查找测试单元所依赖的Bean Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authServerApplication': Unsatisfied dependency expressed through field 'passwordEncoder'; ne
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authServerApplication': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.crypto.password.PasswordEncoder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的测试班:
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest
public class UserDetailsTest {
@Autowired
private TestEntityManager entityManager;
// @MockBean
// private PasswordEncoder passwordEncoder;
@Autowired
private UserRepository userRepo;
@Test
public void test() {
OAuthUser user = null;
this.entityManager.persist(new OAuthUser("Kelly", "Marchewa", "kmarchewa", "password"));
user = userRepo.findByUserName("kmarchewa");
System.out.println(user.getPassword());
assertThat(user.getUserName()).isEqualTo("kmarchewa");
}
}
如果我取消对@MockBean部分的注释,代码将可以很好地编译。但是,我还想测试存储库对密码进行编码和解码的能力。据我所知,@SpringBootTest注释应该能够自动“拾取”@Configuration类。我有一个主要的@springboot应用程序:
@SpringBootApplication
public class AuthServerApplication {
@Autowired
private PasswordEncoder passwordEncoder;
public static void main(String[] args) {
SpringApplication.run(AuthServerApplication.class, args);
}
@Bean
public CommandLineRunner demo(UserRepository repository) {
return(args) -> {
OAuthUser user = new OAuthUser();
user.setFirstName("Kelly");
user.setLastName("Marchewa");
user.setPassword(passwordEncoder.encode("Admin"));
user.setUserName("Admin");
// repository.save(user);
};
}
}
这个Spring引导应用程序依赖于另外三个@Configuration类:AppConfig、SecurityConfig和AuthServerConfig。对于这个问题,SecurityConfig和AppConfig类是相关的(它们包括对PasswordEncoder bean的引用)
AppConfig(部分)
SecurityConfig:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private OAuthUserDetailsService userService;
@Autowired
private PasswordEncoder passwordEncoder;
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
// Hash password
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService)
.passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.httpBasic()
.realmName("test")
.and()
.csrf()
.disable();
}
}
UserRepository类非常简单:
public interface UserRepository extends CrudRepository<OAuthUser, Long> {
public OAuthUser findByUserName(String name);
}
问题是
@DataJpaTest
此注释应仅用于数据存储库测试,而不是完全集成(这是您正在做的),因为它仅在上下文中创建持久性bean,而不是所有您的bean(找不到bean的原因)。您需要做的是只使用@SpringBootTest
,并声明h2
为测试依赖项,这样就可以使用内存数据库创建应用程序的完整副本了,非常感谢您的响应。然而,这提出了另一个问题。我如何表明我希望使用h2(我的类路径上有h2),而不是我选择的生产数据库(PostgreSQL)?我在build.gradle文件中为h2指定了testCompile(),为PostgreSQL指定了compile(),但测试似乎仍然在使用PostgreSQL。有没有一种方法可以覆盖这种行为?在application.properties/yml中的Thance.connection字符串,您可以在测试资源文件夹下添加更新版本。
public interface UserRepository extends CrudRepository<OAuthUser, Long> {
public OAuthUser findByUserName(String name);
}
@Autowired
private PasswordEncoder passwordEncoder;