简体   繁体   English

jUnit测试中不存在grails.plugins.springsecurity软件包

[英]package grails.plugins.springsecurity does not exist in jUnit test

I am working on a grails application. 我正在编写grails应用程序。 In the application views and controllers are only grails components. 在应用程序中,视图和控制器仅是grails组件。 Instead of using domains and services application have DTOs and BLs java files respectively. 而不是使用域和服务应用程序,分别具有DTO和BLs Java文件。

I am writing junit tests in which I required to reauthenticate user using spring security service. 我正在编写junit测试,在这些测试中,我需要使用Spring Security服务重新认证用户。 But when I run the test it give me the following error: 但是,当我运行测试时,出现以下错误:

[javac] ... error: package grails.plugins.springsecurity does not exist
[javac] import grails.plugins.springsecurity.SpringSecurityService;
[javac] ... error: package grails.plugins.springsecurity does not exist
[javac] private SpringSecurityService springSecurityService = ...

I have no idea why I am getting error even in import. 我不知道为什么即使在导入时也会出错。 Code: 码:

import grails.plugins.springsecurity.SpringSecurityService;
....
SpringSecurityService springSecurityService = ...
...

OR 要么

Is there any other better way to reauthenticate user in junit test? 在junit测试中还有其他更好的方法来重新认证用户吗?

I have googled how to authenticate user in junit test but does not found any solution working. 我用谷歌搜索了如何在junit测试中对用户进行身份验证,但是没有找到任何有效的解决方案。

I'm not sure why this particular issue is happening, but you can't really test security with unit tests. 我不确定为什么会发生此特定问题,但是您不能真正使用单元测试来测试安全性。 Spring Security is implemented as a filter chain, so you need to create a request and apply the chain. Spring Security是作为过滤器链实现的,因此您需要创建一个请求并应用该链。

This could be properly mocked, but it would be a lot of work. 可以适当地嘲笑它,但这将需要大量工作。 Your best bet is to use functional tests, with a real running server and real requests. 最好的选择是使用功能测试,真实运行的服务器和真实请求。 The risk of false positives with other approaches is way too high. 其他方法误报的风险过高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM