The Reedemed77
The Reedemed77

Reputation: 459

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);
}}

Upvotes: 4

Views: 7446

Answers (1)

jzheaux
jzheaux

Reputation: 7762

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.

Upvotes: 6

Related Questions