I have a custom Ldap Authentication provider which is not called during my tests due to the mocks(I think). Is there any way to write junit testcases for Authentication provider implementation? Test passes but custom authentication provider configured in security configuration is not called!
Spring config:
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {
@Autowired
private final LdapAuthenticationProvider ldapAuthenticationProvider;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.cors(httpSecurityCorsConfigurer -> httpSecurityCorsConfigurer.configurationSource(corsConfigurationSource()))
.authorizeHttpRequests(request -> request.requestMatchers("/api/v1/4g/**")
.permitAll().anyRequest().authenticated())
.sessionManagement(manager -> manager.sessionCreationPolicy(STATELESS));
return http.build();
}
@Bean
AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Autowired
void registerProvider(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(ldapAuthenticationProvider);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Authentication provider:
public class LdapAuthenticationProvider implements AuthenticationProvider{
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
... SOME AUTHENTICATION LOGIC HERE ...
return authenticated;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
JUnit Test
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = LoginController.class)
@Import(LoginController.class)
@AutoConfigureMockMvc
@ContextConfiguration(classes = {Application.class})
public class LdapAuthenticationProviderTest {
@InjectMocks
private LoginController loginController;
@Autowired
private MockMvc mockMvc;
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
@TestConfiguration
static class AdditionalConfig {
@Bean
public LdapAuthenticationProvider ldapProvider() {
return new LdapAuthenticationProvider();
}
}
@BeforeEach
void init(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/4gapp/");
MockitoAnnotations.openMocks(loginController);
mockMvc = MockMvcBuilders
.standaloneSetup(loginController)
.setViewResolvers(viewResolver)
.alwaysDo(print())
.build();
}
@Test
public void ldapAuthProviderTest() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(post("/api/v1/4g/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andReturn();
}
}