【问题标题】:Spring Securities Test Cannot Find Bean ErrorSpring Securities Test 找不到 Bean 错误
【发布时间】:2020-08-28 19:20:22
【问题描述】:

我正在使用以下代码来测试带有 Spring 证券的 rest 控制器。 WebMvcTest 用于执行测试。我不想使用 SpringBootTest 注解,因为它会使启动整个应用程序上下文的测试变得非常缓慢。

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

}

我收到以下错误:

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

我怎样才能绕过这个? TokenProvider 已导入 WebSecurityConfig。谢谢。

【问题讨论】:

  • 可以显示测试类代码吗?
  • @Deadpool。我进行了更新。谢谢。

标签: java spring-boot junit spring-security


【解决方案1】:

实际上,@WebMvcTest 测试只关注 Spring MVC 组件。

@WebMvcTest 上下文中缺少 bean 定义。因为你不使用@SpringBootTest,所以所有与Spring MVC上下文无关的组件都被忽略了(比如@Component@Service@Repository beans)。

因此,您需要另外添加 TokenProvider 组件,如果您还有其他依赖于 MVC 的 bean:

@TestConfiguration
public class SampleBeanRestControllerTestConfig {

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

}

接下来,导入您的测试配置:

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

   //...

}

除非TokenProvider 有其他依赖项,否则应该可以。如果是这样,您还需要将它们创建为@Beans。或者,您可以考虑使用@MockBean 或在有意义的情况下手动模拟它们。

希望,它会有所帮助。

【讨论】:

  • 您好 Yavusz,感谢您的回复。我修复了这个,然后它会出现更多丢失的豆子。如何保留 Spring 证券的所有文件?
  • 简而言之,恐怕没有办法做到这一点。依赖的bean及其传递的bean应该一个一个地定义或模拟。或者,如果它看起来几乎涵盖了所有 bean,请考虑使用 @SpringBootTest。但是,如果是这种情况,这可能是设计不良和滥用单一职责原则的依赖注入的标志。
  • 原来定义了 4 个 bean。谢谢
  • 很高兴它对您有所帮助!
猜你喜欢
  • 2020-11-11
  • 2017-08-08
  • 1970-01-01
  • 2017-07-12
  • 2016-05-21
  • 1970-01-01
相关资源
最近更新 更多