Search code examples
javaspringspring-bootspring-testspring-test-mvc

@WebMvcTest Error creating bean with name 'securityConfiguration'


I have a spring boot rest application where I use JWT for authentication and it works fine when I just start the application.

I just wanted to add some test cases but I could not solve this problem (I run the test cases with mvn test :

Error states that Spring can not create userDetailsService bean but this seems incorrect, because my application can run without any issue.

Not: I have tried to add userdetailservice mock via @MockBean. But this time spring shows the same error for MyAuthenticationEntryPoint. I think there should be a solution to add all configuration automatically, or avoid them all for this endpoint.

[ERROR] verifyRegisterUserHttpRequest  Time elapsed: 0.008 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed 
through field 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.myProjects.sample.security.MyUserDetailsService' available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  • Here is the test class:
package com.myProjects.sample.controller;

@WebMvcTest(controllers = RegisterController.class)
public class RegisterControllerTest {
    @Autowired
    private MockMvc mockMvc;
    @Autowired
    private ObjectMapper objectMapper;
    @MockBean
    private RegisterService registerService;
    @MockBean
    private ValidationService validationService;

    // 1. Verifying Http Request Matching
    @Test
    public void verifyRegisterUserHttpRequest() throws Exception {
        NewCustomerRequest requestBody = new NewCustomerRequest();
        requestBody.setEmailAddress("sample@sample.com");
        requestBody.setPhoneNumber("01112223344");
        requestBody.setPassword("password");
        requestBody.setPasswordVerify("password");

        mockMvc.perform(post(MyWebUrls.API.API + MyWebUrls.Register.REGISTER_USER)
                .content(objectMapper.writeValueAsString(requestBody))
                .contentType("application/json"))
                .andExpect(status().isOk());
    }
}
  • RegisterController and SecurityConfiguration
package com.myProjects.sample.controller;
@RestController
@RequestMapping(MyWebUrls.API.API)
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private ValidationService validationService;

    @PostMapping(MyWebUrls.Register.REGISTER_USER)
    public ResponseEntity<?> registerUser(@Valid @RequestBody NewCustomerRequest newCustomerRequest, BindingResult bindingResult){
        String validationResultMessage = validationService.getFirstValidationErrorMessage(bindingResult);
        // ...
        }
    }
}
package com.myProjects.sample.security;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        // securedEnabled = true,
        // jsr250Enabled = true,
        prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailsService userDetailsService;

    @Autowired
    private MyAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private CustomAccessDeniedHandler accessDeniedHandler;

    @Bean
    public AuthTokenFilter authJwtTokenFilter(){
        return new AuthTokenFilter();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
    // ....
  • And also see my custom details service class
package com.myProjects.sample.security;
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String emailOrMobilePhone) throws UsernameNotFoundException {
        // ...
    }
}

Solution

  • Can you add the following dependency to your project?

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <scope>test</scope>
    </dependency>
    

    This should then ensure to also populate your security configuration for your @WebMvcTest.

    You can then use several techniques to mock credentials/user when accessing your application with MockMvc:

    @Test
    @WithMockUser("duke")
    

    or

    @Test
    public void testWithSecurityMockMvcRequestPostProcessors() throws Exception {
      this.mockMvc
        .perform(
          post("/your/endpoint")
            .with(jwt())
            .with(csrf())
        )
        .andExpect(status().isOk());
    
    }
    

    More information for the MockMvc and Spring Security integration can be found here.