Created
June 23, 2012 17:16
-
-
Save crazy4groovy/2979077 to your computer and use it in GitHub Desktop.
Groovy MockFor example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://groovy.codehaus.org/Using+MockFor+and+StubFor | |
import groovy.mock.interceptor.* | |
/*sample data class*/ | |
class Movie { | |
// Lots of expensive method calls, etc. | |
} | |
/*helper class*/ | |
class MovieAnalyzer { | |
def isGood( movie ) { | |
if( movie.hasGalifinakis() ) | |
return true; | |
else if( movie.hasSparklyVampires() ) | |
return false; | |
return ( new Date().getTime() / 2 ) == 0 | |
} | |
} | |
/*unit test the helper class by mocking the data class*/ | |
def testTwitlightMovie(){ | |
def mock = new MockFor(Movie) | |
//StubFor is similar to MockFor except that | |
//the expectaion of methods called is order independent ... | |
mock.demand.hasGalifinakis { true } | |
mock.demand.hasSparklyVampires( 0..1 ) { true } // demand the method 0 or 1 times | |
mock.use { | |
assert true == new MovieAnalyzer().isGood( new Movie() ) | |
} | |
} | |
testTwitlightMovie() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment