【发布时间】:2022-01-21 23:07:44
【问题描述】:
当我检查了我找到的文档this 时,我想使用基于方法的安全性对我的控制器安全性进行单元测试,但它使用了@RunWith,而且它似乎并没有只加载安全配置。
我想做什么: 加载我的安全配置
@ContextConfiguration(classes = {SecurityConfigImpl.class})
public class PeopleGQLApiSecutiryTest {
private final PersonService personService = mock(PersonService.class);
private final PeopleGQLApi peopleGQLApi = new PeopleGQLApi(personService);
@Test
@WithAnonymousUser
void itShouldCreateNewPerson() {
when(personService.createNewPerson(any(Person.class))).thenReturn(new Person());
// this should fail!
peopleGQLApi.createPerson(
Person.LevelEnum.WEAK,
// rest of the arguments
);
}
}
这个应该失败(通过抛出一个期望或类似的东西),因为 create person 是一个安全的函数,带有ROLE_ADMIN。
注意:这是一个 GraphQL 突变
@MutationMapping("createPerson")
@Secured({"ROLE_ADMIN"})
public Person createPerson(
@Argument("level") Level level,
// rest of the arguments
) {
// method implementation
}
我不能使用完全成熟的 @SpringBootTest,因为我还必须提供 mongodb 配置,这超出了本测试的范围。
【问题讨论】:
标签: java spring spring-security junit5