Grails unit or integration tests?

不打扰是莪最后的温柔 提交于 2019-12-10 21:02:53

问题


I don't know whether to use unit or integration tests for what I am wanting to test in my Grails 2.2.3 application. I want to run some tests against like this:

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

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

This is going to require the use of our test database, so would that make it an integration test? When I run this code as a unit test it fails at assert s != null. Which means it isn't using our database, because it SHOULD find a student with that ID.


回答1:


In Grails unit test you can test domain class interactions using Gorm and behind the scene Grails will use in-memory database(an implementation of a ConcurrentHashMap) to mimic this behavior here. So yes you get null because that student does not exist in in-memory database and you need to insert that data first.

Student.findOrSaveWhere (firstName: 'Grant',lastName : 'McConnaughey')

In your example, if the intention is to test the existence of that data you need to use integration test and connect it to your database using datasource.groovy , which is really not a good idea unless you have a good reason to test your data.

If you are trying to test def s = Student.findById(myId) again that is not adding any value as that is Grails dynamic finder and you probably need to trust the framework you are using.

However, in general

Unit tests are typically run without the presence of physical resources that involve I/O such databases, socket connections or files link

I hope this helps



来源:https://stackoverflow.com/questions/17657588/grails-unit-or-integration-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!