Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot Http Security config unit test

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);
}}
like image 359
The Reedemed77 Avatar asked Sep 17 '25 01:09

The Reedemed77


1 Answers

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.

like image 188
jzheaux Avatar answered Sep 19 '25 15:09

jzheaux