Java Spring证券测试无法找到Bean错误

Java Spring证券测试无法找到Bean错误,java,spring-boot,junit,spring-security,Java,Spring Boot,Junit,Spring Security,我正在使用以下代码测试带有Spring securities的rest控制器。WebMvcTest用于执行测试。我不想使用SpringBootTest注释,因为它会使启动整个应用程序上下文的测试非常缓慢 package org.project.rest; import com.fasterxml.jackson.databind.ObjectMapper; import org.project.model.SampleBean; import org.project.service.Sampl

我正在使用以下代码测试带有Spring securities的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.

我怎么能绕过这个?已在WebSecurity配置中导入令牌提供程序。谢谢。

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

@WebMvcTest
上下文中缺少bean定义。因为您没有使用
@springbootest
所有与Spring MVC上下文无关的组件都将被忽略(例如
@Component
@Service
@Repository
bean)

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

@TestConfiguration
公共类SampleBeanRestControllerTestConfig{
@豆子
公共令牌提供者令牌提供者(){
返回新的令牌提供者();
}
}
接下来,导入测试配置:

@RunWith(SpringRunner.class)
@导入(SampleBeanRestControllerTestConfig.class)
@WebMvcTest(SampleBeanRestController.class)
@AutoConfigureMockMvc
公共类样本BeanRestControllerTest{
//...
}
除非
TokenProvider
具有其他依赖项,否则这应该是可行的。如果是这样,您还需要将它们创建为
@Bean
s。或者,你可以考虑使用<代码> @ MockBean >代码>或者如果有意义的话手动地模仿它们。


希望有帮助。

您能显示测试类代码吗?@Deadpool。我已经更新了。谢谢。嗨,Yavusz,谢谢你的回复。我修好了这个,然后又发现了更多的缺豆。我如何保存Spring securities的所有文件?简而言之,恐怕没有办法。依赖的bean及其可传递的bean应该逐个定义或模拟。或者,如果它看起来覆盖了几乎所有的bean,请考虑使用<代码> @ SpRunBooTest。然而,如果是这种情况,这可能是一个坏的设计和滥用依赖注入的迹象,在单响应性原则方面。谢谢我很高兴这对你有帮助!