1

在我的项目中,我在登录控制器中设置了 session.loggedInUser。但是在集成测试期间,我们不使用登录控制器。所以我为 session.loggedInUser 设置了值。但我不能在那个地方使用会话。我如何在集成测试中使用会话。对此给出一些解决方案。先感谢您

class MaritalStatusIntegrationTests {

    @Test
    void testCategoryAudit() {

        RequestContextHolder.currentRequestAttributes().session.loggedInUser="Anantha"
            def category = new Category(name:"Single")

        category.save(flush:true)
        assert CategoryAudit.count() == 1

        category.name="Married"
        category.save(flush:true)
        assert CategoryAudit.count() == 2
    }
}

类别.groovy:

class Category {
    static constraints = {
        name blank:false
    }
    String name

    //Auditing
    static auditable = false
    def onSave = {
        new CategoryAudit(this,'Insert').save(failOnError:true)
    }
}

CategoryAudit.groovy:

import org.springframework.web.context.request.RequestContextHolder
class CategoryAudit {
    String name
    String operation
    String doneBy
    Date txnDate
    def CategoryAudit(){}
    def CategoryAudit(Category category , String operation) {
        this.name = category.name
        this.operation = operation
        this.doneBy = RequestContextHolder.currentRequestAttributes().session.loggedInUser
        this.txnDate = new Date()
    }
}
4

1 回答 1

2

没有这样的属性:类的 RequestContextHolder:com.vasco.gs.MaritalStatusIntegrationTest。

只是为了清理,根据 OP,它缺少RequestContextHolder.

于 2013-08-16T16:49:03.067 回答