如何使用MockMvc和Spring Security过滤器为Spring Boot控制器创建集成测试?

huangapple go评论56阅读模式
英文:

How to create an integration test for a Spring Boot controller using MockMvc and Spring Security filters?

问题

我有一个使用Spring Security保护路由的Spring Boot应用程序。每当用户尝试访问一个端点时,都会通过安全过滤器进行用户身份验证。现在,我想创建一个使用MockMvc的控制器集成测试。

我已经按照官方文档的步骤进行了操作,但我一直在遇到与WebSecurityConfig的自定义bean以及相关依赖项有关的错误。我尝试过各种注解的组合,比如@SpringBootTest,@WebMvcTest,@ContextConfiguration和@AutoConfigureMockMvc,但似乎无法使其工作。

我的目标是创建一个控制器集成测试,仅测试控制器层,模拟依赖关系,并使用模拟用户(@WithMockUser)通过身份验证过滤器。

有人可以提供如何设置满足这些要求的集成测试的指导吗?

英文:

I have a Spring Boot application that uses Spring Security to protect routes. Every time a user tries to access an endpoint, it goes through a security filter to authenticate the user. Now, I would like to create an integration test for a controller using MockMvc.

I've followed the official documentation, but I keep getting errors related to a custom bean from the WebSecurityConfig and its related dependencies. I've tried various combinations of annotations such as @SpringBootTest, @WebMvcTest, @ContextConfiguration, and @AutoConfigureMockMvc, but I can't seem to get it working.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class CsrfShowcaseTests {

@Autowired
private WebApplicationContext context;

private MockMvc mvc;

@Before
public void setup() {
	mvc = MockMvcBuilders
			.webAppContextSetup(context)
			.apply(springSecurity()) 1
			.build();
}

...

My goal is to have a controller integration test that tests only the controller layer, mocks dependencies, and uses a mock user (@WithMockUser) to pass the authentication filters.

Can anyone provide guidance on how to set up an integration test that meets these requirements?

答案1

得分: 1

spring-security-test 依赖提供了对集成测试中模拟用户的良好支持。您可以使用


@WithAnonymousUser 或 @WithMockUser

此外,要运行集成测试,您可以:

  • 要么使用(这将启用完整的上下文,并且您可以使用 @MockBean 模拟服务层):

@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)

  • 要么尝试使用以下设置:

@ExtendWith(SpringExtension.class)
@WebMvcTest(YourController.class)
@AutoConfigureMockMvc(addFilters = false)
@ImportAutoConfiguration(classes = {SecurityConfig.class})

希望这有所帮助。

英文:

spring-security-test dependency provides good support for mocking users for integration tests..

You can use


@WithAnonymousUser or @WithMockUser 

Apart from this, to run the integration test you can :

  • Either use (this will enable complete context and you can mock the service layer using @MockBean):
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
  • Or use can try using below setup :
@ExtendWith(SpringExtension.class)
@WebMvcTest(YourController.class)
@AutoConfigureMockMvc(addFilters = false)
@ImportAutoConfiguration(classes = {SecurityConfig.class})

Hope this helps..

huangapple
  • 本文由 发表于 2023年4月4日 16:49:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定