You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@BeforeEach
@WithMockUser(roles = ["ADMIN"])
fun setUp() {
itemRepository.save(Item())
}
@Test
@Disabled
@WithMockUser(roles = ["USER"])
fun `user should not see items through ItemRepository`() {
assertThrows<AccessDeniedException> { itemRepository.findAll() }
}
this test fails beacuse setUp method, executed before it, fails with AccessDeniedException
The text was updated successfully, but these errors were encountered:
AleksandrSl
changed the title
@WithMockUser doesn't work on method annotated as @BeforeEach@WithMockUser doesn't work on method annotated as @BeforeEachMar 3, 2018
@rwinch sorry to resurrect this old issue, but I don't believe this problem has been solved, and also I don't believe it to be a duplicate of the issue you referenced.
The issue you referenced allows to define when the SecurityContext is set: just before test execution OR even before @Before methods.
This issue however is that both the original poster here and I want to have a different security context in the test method and the before method, which doesn't currently seem possible.
Summary
Strange behaviour of
@WithMockUser
on method annotated as@BeforeEach
Actual Behavior
@BeforeEach
annotated method uses security context from@Test
method that will be run after itExpected Behavior
@BeforeEach
should have security context specified in@WithMockUser
annotationVersion
spring-boot-starter-security:2.0.0.RELEASE
(seems to be spring-security:5.0.3.RELEASE)
Sample
Full example
this test fails beacuse
setUp
method, executed before it, fails withAccessDeniedException
The text was updated successfully, but these errors were encountered: