@Skoleev

Во время тестах бустрапит Spring Security и требует userService, как выключить Spring security в тестах или обойти эту проблему?

У меня есть простенький тест, как мы видим, уже добавлены excludeAutoConfiguration и addfilters ( но разницы никакой ). Если я не мокаю бин UserService, то получаю эту ошибку.
Parameter 0 of constructor in com..restaurant.security.SecurityConfig required a bean of type 'com..restaurant.security.UserService' that could not be found.

@WebMvcTest(value = UserRestaurantController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
@AutoConfigureMockMvc(addFilters = false)
class UserRestaurantControllerTest {

    @Autowired
    MockMvc mockMvc;
    @Autowired
    ObjectMapper mapper;

    @MockBean // Если тут не будет mockbean, то выдаст ошибку выше
    RestaurantService restaurantService;



    RestaurantResponseDTO restaurantDominos = new RestaurantResponseDTO(1, "Dominos Pizza", "Басейна, 17", 3);
    RestaurantResponseDTO restaurantMamamia = new RestaurantResponseDTO(2, "Mamamia", "проспект Победы, 9Б", 14);
    RestaurantResponseDTO restaurantAndriano = new RestaurantResponseDTO(3, "Adriano's pizza", "Глибочицкая, 33/37", 2);

    @Test
     void shouldFind3Restaurants_whenGet() throws Exception {
        List<RestaurantResponseDTO> restaurantsResponse = new ArrayList(Arrays.asList(
                restaurantDominos, restaurantMamamia, restaurantAndriano));

        Mockito.when(restaurantService.getAll()).thenReturn(restaurantsResponse);

        mockMvc.perform(get("/restaurant")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(3)))
                .andExpect(jsonPath("$[2].name", is("Adriano's pizza")));
    }

Вот мой провайдер

@Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider daoProvider = new DaoAuthenticationProvider();
        daoProvider.setPasswordEncoder(passwordEncoder());
        daoProvider.setUserDetailsService(userService); // Вот и бин, который ему нужен
        return daoProvider;
    }


Уже не знаю, что делать даже.
  • Вопрос задан
  • 202 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы