Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Grails在使用会话时,如何决定选择request.getSession(true)或request.getSession(false)_Unit Testing_Grails - Fatal编程技术网

Unit testing Grails在使用会话时,如何决定选择request.getSession(true)或request.getSession(false)

Unit testing Grails在使用会话时,如何决定选择request.getSession(true)或request.getSession(false),unit-testing,grails,Unit Testing,Grails,在一个过滤器中,我使用request.getSession(false)来获取一个会话,这样就可以了。但是当我对它进行单元测试时,request.getSession(false)将返回null obj,测试结果失败。如果我想通过测试,我应该使用request.getSession(true)。request.getSession(true)当且仅当一个会话不存在时才创建一个新会话。如果一个会话已经存在,如在生产环境中,它将使用调用代码的Java EE servlet容器中可用的会话 换句话说,

在一个过滤器中,我使用request.getSession(false)来获取一个会话,这样就可以了。但是当我对它进行单元测试时,request.getSession(false)将返回null obj,测试结果失败。如果我想通过测试,我应该使用request.getSession(true)。

request.getSession(true)当且仅当一个会话不存在时才创建一个新会话。如果一个会话已经存在,如在生产环境中,它将使用调用代码的Java EE servlet容器中可用的会话

换句话说,您可以在当前使用request.getSession(false)的生产环境中的任何位置使用request.getSession()(与getSession(true)相同)。在生产环境中不需要执行getSession(false),即getSession(true)将始终返回当前会话(如果存在)。在测试环境中,您当然会得到一个空白会话,然后需要使用运行测试所需的任何数据填充该会话。但在这两个位置,它都应该做正确的事情

请注意,Grails有自己的会话对象,它包装了JavaEE会话对象,因此您可能希望使用它来代替