我正在从头开始创建一个新的Spring Boot应用程序,并希望为其编写测试。我刚刚在我的应用程序中实现了身份验证,并想了解角色的工作方式。

在身份验证过程中使用UserRepository时,一切都会正常进行。但是,当我想在测试中使用UserRepository时,它说该对象为null,这与我在应用程序代码中使用该对象一样可以。这是为什么?
这是代码。

安全配置类:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PowderizeUserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic()
                .and()
                .logout().permitAll();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManager) {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        authenticationProvider.setUserDetailsService(userDetailsService);
        authenticationManager.authenticationProvider(authenticationProvider);
    }

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

}


用户类别:

@Entity
@Table(name = "USERS")
@NoArgsConstructor
@Getter
public class User extends BaseEntity {

    private String firstName;
    private String lastName;
    private String emailAddress;
    private String nickname;
    private String password;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;
    @ManyToMany(mappedBy = "users_roles")
    private Set<Role> roles;
}


仓库:

public interface UserRepository extends CrudRepository<User, Long> {

    public Optional<User> findByEmailAddress(String email);
}


UserDetailsS​​ervice实现类,它使用存储库没有问题:

@Service
public class PowderizeUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
                return new PowderizePrincipal(
                        userRepository.findByEmailAddress(email)
                                .orElseThrow(() -> new UsernameNotFoundException("User '" + email + "' not found."))
                );
    }
}


返回NullPointerException的测试类:

@SpringBootTest
public class UsersAndRolesTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void ww(){
        assertThat(userRepository, notNullValue());
    }

    @Test
    public void userExistsInDatabase(){
        assertThat(userRepository.findByEmailAddress("[email protected]").isPresent(), notNullValue());
    }

}


我尝试使用@ Repository,@ EnableJpaRepositories之类的注释,实际上是我找到的每个解决方案。 IntelliJ还用“无法自动装配。找不到'UserRepository'类型的bean”突出显示userRepository

最佳答案

将此注释添加到您的UsersAndRolesTest类


  @RunWith(SpringRunner.class)

关于java - Spring Data存储库无法 Autowiring ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52481169/

10-13 05:18