我正在尝试测试由@RolesAllowed保护的quarkus其余端点
...
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@RolesAllowed({ "APPLICATION_USER"})
public Response getFile(@PathParam(value = "id") String documentId, @Context UriInfo uriInfo)
...
测试用例
@QuarkusTest
class DocumentResourceTest {
@Test
public void testDocumentEndpoint() {
String documentId = "someId";
given()
.when().get("/documents/" + documentId)
.then()
.statusCode(200);
}
}
如何为测试用例模拟角色为“ APPLICATION_USER”的经过身份验证的用户?
您可以注入一个SecurityIdentity
,然后使用Mockito将其扮演相关角色:
@QuarkusTest
public class DocumentResourceTest {
@InjectMock
SecurityIdentity identity;
@BeforeEach
public void setup() {
Mockito.when(identity.hasRole("APPLICATION_USER")).thenReturn(true);
}
@Test
public void testDocumentEndpoint() {
String documentId = "someId";
given()
.when().get("/documents/" + documentId)
.then()
.statusCode(200);
}
}
如果您想测试各种不同的角色,则当然可以将存根调用移到各个测试中。
请注意,您需要添加quarkus-junit5-mockito
依赖项才能使其正常工作:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句