Uzywam keycloak'a jako server autoryzujacy, ale na czas testow go wylonczylem
keycloak.enabled = false
Wewnatrz w konfiugu probowalem niemal wszystkiego zeby mi weszlo do tego URL'a jako uzytkownik niezalogowany
public class KeyCloakConfig extends KeycloakWebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
http
.anonymous().and() .csrf().disable()
.authorizeRequests()
.antMatchers("/ingredients").permitAll();
}
}
w tescie mam 401 nawet jak zaloncze @WithAnonymousUser
@WebMvcTest(value = IngredientController.class)
@TestPropertySource("classpath:application-development.properties")
class IngredientControllerTest {
@MockBean
IngredientService ingredientService;
@Autowired
MockMvc mockMvc;
@AfterEach
void tearDown() {
reset(ingredientService);
}
@Test
// @WithAnonymousUser
void getAllIngredients() throws Exception {
given(ingredientService.findAll()).willReturn(Arrays.asList(new Ingredient(),new Ingredient()));
mockMvc.perform(get("/ingredients"))
.andExpect(status().isOk());
// .andExpect(content().contentType(MediaType.valueOf("text/html;charset=UTF-8")));
then(ingredientService).should().findAll();
assertThat(ingredientService.findAll()).hasSize(2);
}
}
co zabawne jak uzyje adnotacji @WithMockUser to wszystko jest ok