I recently came across a small shortcoming in Grails' mockFor feature. I wasn't able to return a value from a service that was mocked. I get an error where the return value is always a closure, not the value I intended. (Note this occurred in grails v1.1).
I found that using Groovy's MockFor is just as convenient and does not contain this shortcoming.
Concept Overview
Often a developer would like to use mocks to isolate code and verify that a unit of code is operating correctly. The rationale is: "Given that collaborators are behaving in a specific way, the code under test behaves as expected." In other words, if collaborator returns A, then the code under test will perform B. If the collaborator returns C, then the code under test will perform D.
Many Java mock frameworks provide this ability, including EasyMock and JMock, to name a few.
mockFor Shortcoming
On a grails project, I wanted a mock to return a certain value. This did not work. Let me provide an example to illustrate the problem.
Articles and thoughts on Java technologies, software engineering practices, and agile methods.
Analytics
Showing posts with label mock. Show all posts
Showing posts with label mock. Show all posts
Thursday, December 30, 2010
Sunday, August 15, 2010
3 Common Ways to Use Groovy Closures
What is a Closure?
The concept of closures is an important part of the Groovy language. A closure resembles a function or method in many aspects of programming. For example a closure can take arguments, can have a return value, and can be executed from one or many clients. A closure is essentially a block of code that is defined and executed a later point. A closure can be assigned to a variable or remain anonymous. You can create a closure, assign it to a variable, and then pass it around your program like any other variable. For a more thorough explanation, see Groovy Formal Definition.
Use Groovy Closures
Closures enable the creation of creating concise, clean code when applied correctly. Although all programming problems can be solved without closures, a problem solved using closures will most likely contain less code, avoid repetition, and be more elegant. This article will focus on some simple and common uses of closures. It is directed towards newcomers to Groovy, typically Java developers coming over to the dark side. We will discover how closures can be used for:
The concept of closures is an important part of the Groovy language. A closure resembles a function or method in many aspects of programming. For example a closure can take arguments, can have a return value, and can be executed from one or many clients. A closure is essentially a block of code that is defined and executed a later point. A closure can be assigned to a variable or remain anonymous. You can create a closure, assign it to a variable, and then pass it around your program like any other variable. For a more thorough explanation, see Groovy Formal Definition.
Use Groovy Closures
Closures enable the creation of creating concise, clean code when applied correctly. Although all programming problems can be solved without closures, a problem solved using closures will most likely contain less code, avoid repetition, and be more elegant. This article will focus on some simple and common uses of closures. It is directed towards newcomers to Groovy, typically Java developers coming over to the dark side. We will discover how closures can be used for:
Subscribe to:
Posts (Atom)