Home > Groovy, TDD > Who needs a mock framework?

Who needs a mock framework?

In Groovy, you can create mocks & stubs using native language features, so you can forget about learning a mock framework.

class Author {
  def getName() {
    return 'Josh Brown'
  }
  def isFamous() {
    return false
  }
}

Let’s stub the isFamous method:

def author = [isFamous: {true}] as Author
assert 'Josh Brown' == author.getName()
assert author.isFamous()

(If you haven’t already, stick the above code in your groovyConsole, run it, and play around with it.)

When mocking and stubbing are so easy to do with the language itself, using a mock framework is overkill.

[Update: Bob Martin recently wrote an excellent post about Manual Mocking.]

Advertisements
Tags: , ,
  1. July 27, 2009 at 2:18 am

    Groovy does make it very simple to stub out functions. But how can you verify interactions?

    For example, using Mockito’s testspy functionality on some Java classes:

    interface EmailService {
    void sendMail(String subject, String body);
    }

    class ServiceConsumer {
    // Method to be tested
    void doSomething() {

    emailService.sendMail(“subject”, “body”);

    }
    }

    @Test
    void shouldSendEmail() {
    consumerService.doSomething();
    // Check the emailService collaborator was called with the correct arguments.
    verify(emailService).sendMail(“subject”, “body”);
    }

  2. faenvie
    July 27, 2009 at 3:43 am

    IMO mocking with gmock is clearly superior
    to groovy’s onbord-mocking.

  3. July 27, 2009 at 3:53 am

    I’ve heard GMock is good, looking to try either GMock or Spock, any recommendations?

  4. Marc Peabody
    July 27, 2009 at 7:16 am

    I think the point of Josh’s post is to utilize Groovy rather than a framework when your testing is simple enough to do so. Wanting to test multiple services interacting with one another is probably a good pressure point to start considering a framework.

    The temptation coming from Java is to rely on frameworks to do everything for you and never really think much for yourself. With Groovy, you’ve a much more powerful language that lets you do a lot of things Java couldn’t do. If a couple lines of code can replace your need for an entire framework, it’s possible that the framework isn’t worth using, even if it does look nice on a resume.

  5. July 27, 2009 at 12:09 pm

    Well said Marc. Josh definitely has a point.

    Its easy to forget that you can just use ducks.. I mean maps.

    Mock Frameworks are important when you want to verify that the artifact that you are testing is exercising certain code paths of a mocked object. Its good to know when you ought to use a mock vs a map.

  6. July 27, 2009 at 1:31 pm

    @Richard Paul

    Actually, you can also verify interactions with plain Groovy. Using your example:

    @Test
    void shouldSendEmail() {
      def wasCalled = false
    
      def emailService = [sendMail: {subject, body ->
        wasCalled = true
        assert "subject" == subject
        assert "body" == body
      }] as EmailService
    
      consumerService.emailService = emailService
    
      consumerService.doSomething()
      assert wasCalled
    }
    
  7. July 29, 2009 at 1:04 pm

    Groovy’s built-in mocking is definitely usable but has some limitations. One is that it only works if the class under test is written in Groovy. With GMock and Spock, you can also test Java classes.

  8. July 29, 2009 at 4:15 pm

    @Peter – all of the code here works if the class under test is written in Java – I tested it. Can you give an example of when the class under test cannot be written in Java?

  9. Marc Peabody
    October 28, 2009 at 7:58 am

    http://twitter.com/unclebobmartin/status/5228631143 “Mockito is very pleasant as java mocking tools go. But it’s still usually easier to write a simple stub by hand.” – Uncle Bob Martin

  10. November 4, 2009 at 5:36 am

    thanks, your post helped me ! Keep on blogging ! :)

  1. October 29, 2009 at 7:53 pm
  2. December 31, 2009 at 3:17 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: