Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3


GMock is a mocking framework for the Groovy language.

GMock is all about simple syntax and readability of your tests so you spend less time learning the framework and more writing code. To use GMock just drop the gmock jar file in your classpath.

Getting Started

First extend the org.gmock.GMockTestCase. Create mock object using the mock() method. You setup expectation simply by calling method on your mock.

Code Block
def mockLoader = mock()

The code you are testing should be executed within the play closure.

Code Block
void testBasic(){
  // create mock and setup expectation
  play {
    // run your code

In a nutshell

  • Method mocking: mockLoader.load("fruit").returns("apple")
  • Exception mocking: mockLoader.load("unknown").raises(new RuntimeException())
  • Stub mocking: mockLoader.load("fruit").returns("apple").stub()
  • Static method mocking: mockMath.static.random().returns(0.5)
  • Property mocking:"loader")
  • Constructor mocking: def mockFile = mock(File, constructor('/a/path/file.txt'))
  • Partial mocking: mock(controller).params.returns(id: 3)
  • Times expectation: mockLoader.load("fruit").returns("apple").atLeastOnce()
  • Custom matcher: mockLoader.load(match{ it.startsWith("fru") })
  • Strict ordering: ordered { ... }
  • Optional support for Hamcrest matcher: mockLoader.put("test", is(not(lessThan(5))))
  • GMockController if you can't extend GMockTestCase in your test

Visit the website for a full documentation: GMock