小编gra*_*hey的帖子

Grails单元或集成测试?

我不知道是否要在我的Grails 2.2.3应用程序中使用单元测试或集成测试来测试.我想像这样运行一些测试:

@TestFor(Student)
@Mock(Student)
class StudentTests {

void testFoundStudent() {
        def s = Student.findById(myId)
        assert s != null
        assert s.firstName = 'Grant'
        assert s.lastName = 'McConnaughey'
    }
}
Run Code Online (Sandbox Code Playgroud)

这将需要使用我们的测试数据库,那么它会进行集成测试吗?当我将此代码作为单元测试运行时,它失败了assert s != null.这意味着它没有使用我们的数据库,因为它应该找到一个具有该ID的学生.

testing grails junit integration-testing unit-testing

3
推荐指数
1
解决办法
957
查看次数

如何更改Grails域类ID字段的名称?

我有一个Grails 2.2.3域类FundType,我试图映射到遗留数据库表.它有两个字段:codedescription.我想在任何时候使用域类并且最好在任何生成的脚手架上id调用code.但每次我使用名称键时,id我都会遇到此异常:

| Error 2013-07-24 09:38:44,855 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Error evaluating ORM mappings block for domain [com.company.scholallow.FundType]:  null
Message: Error evaluating ORM mappings block for domain [com.company.scholallow.FundType]:  null
Run Code Online (Sandbox Code Playgroud)

这是我的域类包含的内容:

class FundType {

    String id
    String description

    static mapping = {
        id column: 'fund_code', generator: 'assigned', name: 'code'
        description column: 'fund_desc'
    }
}
Run Code Online (Sandbox Code Playgroud)

每当我使用FundType实例时,我都想调用代码fundTypeInstance.code和NOT fundTypeInstance.id.这将使我对用户更友好,因为我正在处理一些叫做代码的东西,而不是id.

所以我想知道我想做的事情是什么?我的域类中导致此ORM映射错误的错误是什么?

编辑:

好的,所以我将我的域类更改为以下内容,并且我找到了ID null null错误的FundType.

class …
Run Code Online (Sandbox Code Playgroud)

orm grails grails-orm hibernate-mapping grails-domain-class

3
推荐指数
1
解决办法
4176
查看次数

在Grails中注入不同的服务

是否有可能让两个服务实现相同的接口并在运行时决定为Grails中的接口注入哪些服务?

例如

MyAService implements MyInterface {
...
}

MyBService implements MyInterface {
...
}
Run Code Online (Sandbox Code Playgroud)

其他服务然后只是引用MyInterface,你决定基于配置设置或注入什么服务?

grails dependency-injection

3
推荐指数
1
解决办法
353
查看次数

创建DRY枚举

我想使用Groovy 2.1.9在几个不同的枚举之间共享类似的功能.枚举都用于生成XML,因此我给它们一个名为的属性xmlRepresentation.以下是两个枚举:

enum Location {
    CollegeCampus('College Campus'), HighSchool('High School'), Online('Online')

    Location(String xmlRep) {
        this.xmlRepresentation = xmlRep
    }

    String toString() {
        xmlRepresentation
    }

    String xmlRepresentation
}


enum InstructorType {
    CollegeFaculty('College Faculty'), HighSchoolFaculty('High School Faculty')

    InstructorType(String xmlRep) {
        this.xmlRepresentation = xmlRep
    }

    String toString() {
        xmlRepresentation
    }

    String xmlRepresentation
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我必须在这两个枚举中声明xmlRepresentation属性,toString方法和构造函数.我想分享这些属性/方法,但我认为我不能继承枚举.我试过没有任何运气使用mixin:

class XmlRepresentable {

    String xmlRepresentation

    XmlRepresentable(String xmlRepresentation) {
        this.xmlRepresentation = xmlRepresentation
    }

    String toString() {
        this.xmlRepresentation
    }
}


@Mixin(XmlRepresentable)
enum Location {
    CollegeCampus('College …
Run Code Online (Sandbox Code Playgroud)

java groovy enums

2
推荐指数
1
解决办法
316
查看次数