In My Spring boot application, I have a security config class for which I am trying to write a unit test for. This is my first time doing it so I need some assistance. Here is the code below. Please some help would be much appreciated. Thank you
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private String id;
private String pwd;
private String role;
@Autowired
private AuthenticationEntryPoint authEntryPoint;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.anyRequest().authenticated()
.and().httpBasic()
.authenticationEntryPoint(authEntryPoint).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
//This allows to view h2 console during development
http.headers().frameOptions().sameOrigin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().
withUser(id).
password(pwd).
roles(role);
}}
I wouldn't recommend writing a unit test for the configuration class itself. Generally, an integration test that proves the functionality of your app, say using Mock MVC, works best.
I realize this isn't what you asked; however, if you take a look at the spring security repo, you'll see that this is precisely how they do it, too. i.e., to test their configurers, they use integration tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With