Shortlink

Picking up Scala

I generally like to pick up one new language and technology every year. And these days the language that has me hooked is Scala. My interest in Scala shot up after the advent of social media web sites where concurrency is the biggest challenge.

I am reading two books on Scala right now.

Martin Odersky’s Programming in Scala 2nd edition.

Venkat Subramaniam’s Programming Scala.

The syntax is a bit complex compared to Groovy, but I think it’s just a matter of time you get over it. I plan to churn out some serious code in Scala in days to come.

Shortlink

Spock for Groovy – IV

In this last post of using Spock with Groovy, let’s learn to use mock objects.
We’ll continue to use the Calculator class. The Calculator class has the add method that returns the sum of two numbers. Let’s modify the add method to store the results of the operation in a store. For this purpose, we’ll use an interface called CalcStore that has a store method as shown below.

//CalcStore.groovy
interface CalcStore {
	boolean store(operation,num1,num2,result)
}

The Calculator class that uses the CalcStore is shown below.

 
//Calculator.groovy
class Calculator {
	CalcStore calcStore
	def add(num1,num2){
		def sum = num1 + num2
		storeResults("Addition",num1,num2,sum)
		sum
	}
	def storeResults(operation,num1,num2,result){
		calcStore.store(operation, num1, num2, result)
	}
}

Let’s create the CalculatorSpec with a feature to test the add method. We’ll mock the behaviour of store function here as shown below.

class CalculatorSpec extends Specification{
	Calculator calc
	def calcStoreMock
	def setup(){
		calc = new Calculator()
		calcStoreMock = Mock(CalcStore)
		calc.calcStore = calcStoreMock
	}
	def "store results"(){
		given:
			1 * calcStoreMock.store("Addition",50,50,100) >> true
			calc.calcStore = calcStoreMock
				
		expect:
			calc.add(50, 50)
	}
	def cleanup(){
		calc = null;
	}
}

In the setup method we mock the CalcStore and assign it to the calculator instance. In the given block of the “store results” feature, we record the behaviour of store function to accept the 4 parameters and return true. The “1 * calcMockStore” indicates that the method will be called once.

This is just a simple example of mocking in Groovy.

Shortlink

Spock for Groovy – III

Continuing with the series of posts on Spock framework for Groovy, let’s discuss testing the exceptions for the Calculator class. The Calculator class with two methods add and divide are given below.

//Calculator.groovy
class Calculator {
	def add(num1,num2){
		num1 + num2
	}
	def divide(num1,num2){
		if(num2 == 0)
			throw new Exception("Invalid argument. Cannot be zero")
		num1/num2
	}
}

Say you want to test the divide function in the CalculatorSpec.groovy. Spock provides two functions that thrown and notThrown that can be used to test the exception rules. Let’s write a feature to check if the exception is thrown and another feature to check if the exception is not thrown. The code is given below.

class CalculatorSpec extends Specification{
	Calculator calc
	def setup(){
		calc = new Calculator()
	}
	def cleanup(){
		calc = null;
	}
       def "divide by zero"(){
		when:
			calc.divide(12, 0)
		then:
			Exception e = thrown(Exception)	
			e.message == "Invalid argument. Cannot be zero"
	}
	def "divide"(){
		when:
			calc.divide(12, 10)
		then:
			notThrown(Exception)
	}
}

In the code above we have two feature methods “divide by zero” that tests if the exception is thrown and also the error message, “divide” that can be used to test if the exception is not thrown in case of valid arguments.

In the last three posts we’ve discussed using various features of Spock framework. In the next post we’ll discuss mocking behavior with Spock.

Shortlink

Spock for Groovy – II

In the previous post, we got introduced to Spock framework. We created a Calculator class with an add method and a spec called CalculatorSpec with a feature to test the add method.

Let’s add some more test features in our CalculatorSpec to see the use of Spock. The Calculator.groovy that we want to spock is given below.

class Calculator {
	def add(num1,num2){
		num1 + num2
	}
}

Let’s write a test feature where we pass a set of numbers and test for the addition result. In this feature, we’ll use the ‘where‘ block. The CalculatorSpec.groovy with this feature is shown below.

//CalculatorSpec.groovy
import spock.lang.Specification;

class CalculatorSpec extends Specification{
	Calculator calc
	def "adding a set of numbers"(){
		given:
			calc = new Calculator()
		expect:
			calc.add(num1,num2) == sum
		where:
			num1 << [1,2,3]
			num2 << [30,40,50]
			sum << [31,42,53]
	}
	//We wrote this feature in the previous post
        def "addition of 2 numbers"(){
		given:
			calc = new Calculator()
		expect:
			31 == calc.add(15,16)	
	}
}

The feature “adding a set of numbers” has a where block with 3 lists. The block will be run calc.add(num1,num2) == sum will be run iteratively by picking one item from each of these 3 lists. Pretty intuitive!!!

Let’s refactor the features to initialize the calc object in setup and cleanup methods as shown below.

import spock.lang.Specification;

class CalculatorSpec extends Specification{
	Calculator calc
	def setup(){
		calc = new Calculator()
	}
	def cleanup(){
		calc = null;
	}
	def "addition of 2 numbers"(){
		expect:
			31 == calc.add(15,16)	
	}
	def "adding a set of numbers"(){
		expect:
			calc.add(num1,num2) == sum
		where:
			num1 << [1,2,3]
			num2 << [30,40,50]
			sum << [31,42,53]
	}
}

We’ll continue learning more…

Shortlink

Spock for Groovy – I

The DSL capability of Groovy is a well known fact. When it comes to writing test cases for Groovy/Grails applications the API has always been not a very pleasant one. People talk about the unit tests being the best documentation for the code you write, but the assert methods and the lines of preparatory code before these assert conditions make the test cases poorly readable. Enter Spock.

Spock is a testing and specification framework. The USP of Spock is the expressive syntax, harnessing the power of DSL. You can write specifications( or specs) in Spock and run it using JUnit.

To begin with, you can create a Groovy project and add spock library and JUnit libraries to your build path. In my project I have spock-core-0.6-groovy-1.8.jar added to the classpath. The groovy compiler version is 1.8.
You can create a simple Calculator class with add method as shown below.

//Calculator.groovy
class Calculator {
	def add(num1,num2){
		num1 + num2
	}
}

We would anyday prefer building the Calculator class using TDD, but that’s not the intention of this post. Let’s create a specification called CalculatorSpec and describe what we want to do using Spock. The CalculatorSpec would extend Specification class. A spec is composed of features. Each feature is implemented as a simple method that’s made up of blocks.
To keep it simple, the features are like the test methods inside a testcase. The CalculatorSpec with a feature is shown below.

//CalculatorSpec.groovy
import spock.lang.Specification;

class CalculatorSpec extends Specification{
	Calculator calc
	def "addition of 2 numbers"(){
		given:
			calc = new Calculator()
		expect:
			31 == calc.add(15,16)	
	}
}

The CalculatorSpec has a feature called “addition of 2 numbers”. You can compare this with the traditional test method such as def testAdditionOf2Numbers(). The feature method has the given and expect blocks. You can compare the expect block with the traditional assertTrue method. Running this spec as a JUnit test case gives you the expected results.

We’ve just scratched the surface of Spock. In the next few articles let’s build this CalculatorSpec to witness the real power of Spock.