简体   繁体   中英

Spring Securities Test Cannot Find Bean Error

I am using the following codes to test rest controller with Spring securities. WebMvcTest is been used to perform test. I do not want to use SpringBootTest annotation as it will make the testing very slow to start the entire application context.

package org.project.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.project.model.SampleBean;
import org.project.service.SampleBeanService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class) // tells JUnit to run using Spring’s testing support.
@WebMvcTest(SampleBeanRestController.class)
@AutoConfigureMockMvc
public class SampleBeanRestControllerTest  {

   @MockBean
   private SampleBeanService SampleBeanService;

   @Autowired
   private MockMvc mockMvc;

   public MockMvc getMockMvc() {
      return mockMvc;
   }

   @Test
   @WithMockUser(username = "user", password = "password", roles = "USER")
   public void deleteSampleById() throws Exception{
      getMockMvc().perform(delete("/api/sample/1")
         .contentType(MediaType.APPLICATION_JSON))
         .andExpect(status().isOk());
   }

}

And I get the following error:

Parameter 0 of constructor in org.project.config.WebSecurityConfig required a bean of type 'org.project.security.jwt.TokenProvider' that could not be found.

How can I bypass this? TokenProvider has been imported in the WebSecurityConfig. Thanks.

Actually, a @WebMvcTest test focuses only on Spring MVC components.

You have a missing bean definition in your @WebMvcTest context. Because you don't use @SpringBootTest all the components which are not related to Spring MVC context ignored (like @Component , @Service or @Repository beans).

Therefore, you need to add your TokenProvider component additionally, and if you have any other beans also which depend on MVC:

@TestConfiguration
public class SampleBeanRestControllerTestConfig {

    @Bean
    public TokenProvider tokenProvider() {
        return new TokenProvider();
    }

}

Next, import your test config:

@RunWith(SpringRunner.class)
@Import(SampleBeanRestControllerTestConfig.class)
@WebMvcTest(SampleBeanRestController.class)
@AutoConfigureMockMvc
public class SampleBeanRestControllerTest  {

   //...

}

That should work unless the TokenProvider has other dependencies. If so you need to create them also as @Bean s. Alternatively, you can consider using @MockBean or mocking them manually if it makes sense.

Hope, it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM