Writing unit and integration tests in grails.

Posted By : Shakil Pathan | 13-Dec-2016

Hi,

In this blog, I'm going to explain you about writing test cases for your grails application. Spock is the default test framework in Grails 2.3+ onwards.

Unit testing are tests at the "unit" level. In other words you are testing individual methods or blocks of code without consideration for surrounding infrastructure. Unit tests are typically run without the presence of physical resources that involve I/O such databases, socket connections or files. This is to ensure they run as quick as possible since quick feedback is important.

While, Integration tests differ from unit tests in that you have full access to the Grails environment within the test.

Below are the two tests, first one is unit test and other is integration test, for a controllers action.

package com.example

import grails.plugin.redis.RedisService
import grails.test.GrailsMock
import grails.test.mixin.*
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
 */
@Mock([SettingsService, DataCacheService, BaseService])
class DashboardControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }
	
	void "test updateBeginningEndingCash"() {
		given:
			/*Mocking Domain Objects*/
			Location locationObject = new Location(storeNumber: "1", name: "Lubbock");
			MasterKey masterKeyObject = new MasterKey(name: Constants.BEGINNING_CASH);
			MasterKeyValue masterKeyValueObject = new MasterKeyValue(masterKey: masterKeyObject)
			/*Mocking Domain Objects Static Methods*/
			GrailsMock mockLocation = mockFor(Location)
			mockLocation.demand.static.get() { Long id ->
				return locationObject;
			}
			/*Mocking Domain Object createCriteria*/
			Expando myCriteria = new Expando();
			myCriteria.list = { Closure  cls -> 
				[masterKeyValueObject]
			}
			MasterKeyValue.metaClass.static.createCriteria = {myCriteria }
			GrailsMock mockMasterKey = mockFor(MasterKey)
			mockMasterKey.demand.static.findAllByNameInList() { List<string> names ->
				return [];
			}
			/*Mocking RedisService Methods*/
			GrailsMock mockRedisService = mockFor(RedisService)
			mockRedisService.demand.deleteKeysWithPattern(4) { String keyString ->
				return null;
			}
			controller.dataCacheService.redisService = mockRedisService.createMock()
			
		when:
			params.date = '12/01/2016'
			session.locationId = 17L
			controller.updateBeginningEndingCash()
			
		then:
			assertEquals "text/csv", response.contentType
			assertEquals 9000f, response.text.split("\n")[1].split(",")[1].toFloat()
	}
}

And below is the integration test for the same:

package com.example

/**
 *
 */
class DashboardControllerTests extends GroovyTestCase {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }
	
	void "test updateBeginningEndingCash"() {
		DashboardController dc = new DashboardController()
		dc.params.date = '12/01/2016'
		dc.session.locationId = 17L
		dc.updateBeginningEndingCash()
		assertEquals "text/csv", dc.response.contentType
	}
}

Hope this helps you in some way.

Any comments would be highly appreciated.

 

Thanks

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..