@WebMVCTest не может найти кастомный бин UserDetailsService, почему?

Вот мой UserDetailsService

    @Service
public class MyUserDetailsService implements UserDetailsService {
    UserRepository userRepository;

    @Autowired
    public MyUserDetailsService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    User user = userRepository.findByEmail(email);
    if(user == null) {
        throw new UsernameNotFoundException("No customer found with the email " + email);
    }
    return new AuthenticatedUser(user);

}

Я создал бин,чтобы в тестах был другой UserDetailsService. AuthenticatedUser implementes UserDetails, поэтому его можно вернуть.

 @TestConfiguration
public class TestConfig {

    @Primary
    @Bean
    public UserDetailsService MyUserDetailsService() {
        return new UserDetailsService() {
            @Override
            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
                return new AuthenticatedUser(UserTestModel.getBasicUser());
            }
        };
    }
}

И импортировал это в простейший тест

    WebMvcTest(UserController.class)
@Import(TestConfig.class)
@WithUserDetails(USER_USERNAME)
public class UserDishControllerTest {

    MockMvc mockMvc;

    ObjectMapper objectMapper;

    @MockBean
    UserService userService;

    @Autowired
    public UserDishControllerTest(MockMvc mockMvc) {
        this.mockMvc = mockMvc;
    }
    @Test
    void createUser() throws Exception {

        var userCreation = getDtoCreationUser();

        var mockRequest =
                post("/signup")
                        .flashAttr("user", userCreation)
                        .with(csrf());

        mockMvc.perform(mockRequest)
                .andExpect(status().isOk());
        Mockito.verify(userService, times(1)).register(userCreation);
    }

}

Но это не работает, я уже буквально копировал код с прошлого проекта, на котором все работало. И все равно выдает эту ошибку

Parameter 0 of constructor in com.exampleepam.restaurant.security.SercurityConfig required a bean of type 'com.exampleepam.restaurant.security.MyUserDetailsService' that could not be found.

No qualifying bean of type 'com.exampleepam.restaurant.security.MyUserDetailsService' available

Помогите, пожалуйста, уже не знаю, что я делаю не так. Уже отчаялся.


Ответы (0 шт):