1

I am new to Mockito, following this and am trying to find out what verify method does. It seems it is used to make sure the selected method is called once. I have the following code, and by looking at the code I can find out I am calling addBook method twice so why should I use verify? I mean in any case it is easy to make sure a method is called oncem so why should we use verify method?

String isbn = mockedBookDAL.addBook(book1);
        assertNotNull(isbn);
        isbn = mockedBookDAL.addBook(book1);
        assertNotNull(isbn);
        verify(mockedBookDAL).addBook(book1);
        assertEquals(book1.getIsbn(), isbn);
Community
  • 1
  • 1
Jack
  • 6,430
  • 27
  • 80
  • 151
  • Why are you mocking the CUT? What does `mockedBookDAL.addBook` actually test? You know it was called **because you called it**. – Boris the Spider Oct 05 '14 at 10:27
  • what do yo mean by CUT? I am just trying to make sure I can add a book. – Jack Oct 05 '14 at 10:30
  • The Class Under Test. You never mock the CUT, you mock its _dependencies_. You then stub these dependencies and verify that the CUT called the expected methods on the dependency. You are not testing very much as far as I can see. – Boris the Spider Oct 05 '14 at 10:32
  • @BoristheSpider I am following this http://java.dzone.com/articles/getting-started-mocking-java Do you know of a better tutorial to learn mockito? – Jack Oct 05 '14 at 10:44
  • @BoristheSpider even in google tutorial it is mocking CUT, http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html – Jack Oct 05 '14 at 10:45
  • Nope, it is not. The google tutorial is just demonstrating how the methods work, not how to write tests. – Boris the Spider Oct 05 '14 at 10:48
  • Sometimes you use it for verifyZeroInteractions.. So you check a code is never executed.. Sometimes the opposite.. – Koray Tugay Oct 05 '14 at 10:49
  • possible duplicate of [When to use Mockito.verify()](http://stackoverflow.com/questions/12539365/when-to-use-mockito-verify) – Joe Oct 05 '14 at 10:49
  • @Joe it is not a duplicate as the questionaire did not accept any of the answers. – Jack Oct 05 '14 at 10:55
  • @BoristheSpider so do you know of any good tutorial to learn Mockito? – Jack Oct 05 '14 at 11:08

1 Answers1

4

imagine a class to manage an account:

public class Account {
  private Logger logger;
  public Account(Logger logger) {
    this.logger = logger;
  }
  ...

  public void withdraw(int amount) {
    ...
    logger.logWithdrawal(amount);
    ...
  }
}

so to test, that the withdrawal was indeed logged, you mock the logger and verify the interaction:

public class AccountTest {
  @Test
  public void withdrawalShouldBeLogged() {
    Logger logger = mock(Logger.class);
    Account cut = new Account(logger);

    int amount = 10;
    cut.withdraw(amount);

    verify(logger).logWithdrawal(amount);
  }
}

This form of asserting is also called a spy.

A further notice: you should generally assert only one thing per test method. Verifying a spy interaction would be that assertion, so you should generally not use verify and assert in the same method.

Max Fichtelmann
  • 3,366
  • 1
  • 22
  • 27
  • Thanks, I have two questions, first of all BoristheSpider mentioned in the comments above that I should not mock the CUT method. Secondly, in this code you would make sure that logwithdrawal method that is located in withdraw method is called by value 10, am I right? – Jack Oct 05 '14 at 11:12
  • right. the cut uses the mock. this test verifies, that the logger is called whenever a withdrawal happens. – Max Fichtelmann Oct 05 '14 at 11:16
  • if that is right, how come you mocked the logger class? – Jack Oct 05 '14 at 11:20
  • When i am testing the Account class, i want to avoid to be separated from problems in other classes. If the logWithdrawal method of the logger has a bug, my tests on the Account should not fail. They should only fail, if the Account behaves in a way, it should not. – Max Fichtelmann Oct 05 '14 at 11:25
  • Got it, I am following this tutorial, http://java.dzone.com/articles/getting-started-mocking-javamay I know how to test BookDAL class? – Jack Oct 05 '14 at 11:27
  • 1
    i have just looked into that example and I think, it is more of a show of mockito, instead of a valid szenario. Data access layers are often mocked, but usually for testing classes, that use them. – Max Fichtelmann Oct 05 '14 at 12:21