java - Spring Securities 测试找不到 Bean 错误
问题描述
我正在使用以下代码来测试带有 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.
我怎样才能绕过这个?WebSecurityConfig 中已导入 TokenProvider。谢谢。
解决方案
实际上,@WebMvcTest
测试只关注 Spring MVC 组件。
您的@WebMvcTest
上下文中缺少 bean 定义。因为您没有使用@SpringBootTest
所有与 Spring MVC 上下文无关的组件(例如@Component
,@Service
或@Repository
bean)。
因此,您需要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
除非有其他依赖项,否则这应该有效。如果是这样,您还需要将它们创建为@Bean
s。@MockBean
或者,如果有意义,您可以考虑手动使用或模拟它们。
希望能帮助到你。
推荐阅读
- html - 如何覆盖 ant-design cechkbox 以使用 font-awesome 图标?
- c# - 在 asp.net MVC Web 应用程序中捆绑和缩小 ES6 javascript 文件
- python - Toad 工作正常,但 cx_oracle 不正常
- laravel - Laravel - 有没有办法从不同队列上的另一个排队作业运行排队作业?
- regex - 如何在两个定义明确的模式之间匹配最多 n 个自由字符的字符串?
- amazon-web-services - 如何根据 AWS API Gateway 中的请求标头动态设置集成端点?
- tableau-api - 根据值更改文本框中的文本
- google-bigquery - Bigquery - 选择最后 # 个不同的值
- ansible - 运行 ansible-playbook 时获取主机名
- ruby-on-rails - 设计 JWT “您需要先登录或注册才能继续”