单元测试 JAX-RS (RESTEasy)
我开发了一个基于RESTEasy(JAX-RS)的REST应用服务器。
我对基于 org.jboss.resteasy.mock.MockDispatcherFactory 的每个服务进行了一组单元测试。这些模拟了数据库调用,以便我可以返回“预设”数据库响应。
一切都运行良好,直到我添加了检查 DIGEST 主体的安全代码 (java.security.Principal
)。我编写了一个安全拦截器,它期望检索调用该服务的用户的用户身份,并验证该用户是否有权执行该服务。
我找不到任何方法发送模拟校长。
除了在单元测试中禁用安全检查之外,有什么方法可以告诉模拟服务器使用模拟主体吗?我宁愿不禁用安全性,因为这是我想要测试的一部分。
I have developed a REST application server based on RESTEasy (JAX-RS).
I have a set of unit tests for each service based on org.jboss.resteasy.mock.MockDispatcherFactory
. These mock out the database calls so that I can return "canned" database responses.
Everything worked well until I added security code that checked the DIGEST Principal (java.security.Principal
). I wrote a security interceptor that expects to retrieve the user identity of the user calling the service and verify that this user has authorization to execute the service.
I can't find any way to send in a mock Principal.
Short of disabling my security checks in my unit tests, is there any way to tell the mock server to use a mock Principal? I would rather NOT disable the security, because that is part of what I want to test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用 Mockito。 Mockito 使您能够模拟调用任何方法,因此很容易产生由您捏造的“假”Principal 对象。请参阅此示例 或谷歌“java.security.Principal 使用mockito进行模拟”以获取更多示例。
我希望这会有所帮助,因为我不能 100% 确定您可以在您的情况下应用这种方法。
I would use Mockito. Mockito enables you to mock call to any method, so it is quite easy to produce "fake", fabricated by you, Principal object. See this example or google "java.security.Principal mocking with mockito" for more examples.
I hope this would help, since I am not 100% sure you can apply such method in your case.