简体   繁体   中英

Testing a method that returns an Object with Mockito

I would like to test makeBooks() method that returns a book object with Mockito. So the test will check whether it returns a book object or not. How do I do that?

Below is my factory that produces books. Below that class is my Mockito test class. I got an error code saying "I misused the Mockito"

I am learning Mockito by the way. This is neither Homework or assignment.

public class ItemFactory {
    public Items getItems(String itemType)
    {      
        public static Books makeBooks(String bookName,String authorName, int ISBN, int id)
        {
            Books book = new Books(bookName, authorName, ISBN, id);
            return book;
        }
    }
}

public class TestItemFactory {
    private static final Books books = null;
    private static ItemFactory mockItemFactory;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        mockItemFactory = Mockito.mock(ItemFactory.class);
    }

    @Test
    public void testShouldReturnObjectBooks(){
        //ItemFactory methodMakeBooks = Mockito.mock(ItemFactory.class) ;
        //Books book = null;// = ItemFactory.makeBooks("Hamlet", "ShakeSpear", 123456, 9);
        //final Class<?> bookClass = Books.class;
        //Mockito.when(methodMakeBooks.makeBooks(null, null, 0, 0)).thenReturn(book);
        Books book = null;

        mockItemFactory = Mockito.spy(new ItemFactory());
        Books mockBook = Mockito.mock(Books.class);
        Mockito.when(mockItemFactory.makeBooks(null, null, 0, 0)).thenReturn(book);
        //Mockito.doReturn(mockBook).when(mockItemFactory).makeBooks(null, null, 0, 0);
    }
}

You should not be mocking the class that is under test. You must be mocking the classes that you class/methods being tested is using. It doesn't make sense to mock Itemsfactory.makeBooks()

If there was a Bookfactory which ItemsFactory was using to make books then you could have mocked BookFactory to return a particular Book/set of books that you were expecting. and then you could assert on that

Make a utility method that runs makeBooks() to check to see whether the value of the Books objects is null and see if it is actually of type Books :

public boolean test(String bookName, String authorName, int ISBN, int id)
{
    // Check to see if the object being returned is not null
    if (ItemFactory.makeBooks(bookName, authorName, ISBN, id) != null)
    {
        // Check to see if object being returned is of type Books
        if (ItemFactory.makeBooks(bookName, authorName, ISBN, id) instanceof Books)
        {
            return true;
        }

        return false;
    }

    return false;
}

As it's already been explained, you should not mock the class you're testing. If Book is a POJO, then there is no need to mock anything, you just need to assert that the created Book object has been populated correctly. Why complicate life when there's a simpler alternative, mock objects only when it's absolutely necessary.

I understand you're learning Mockito, and I think that understanding when and what to mock is also a step in the right direction.

You are probably getting the error because Mockito does not support mocking of static methods. The Powermock extension adds support for static methods, constructors, private methods and so on.

As for examples, the best place to start from is the examples and documentation section hosted on the project's site.

I suggest using PowerMockito (for static method mocking) with Mockito with Junit (for Asserts). I was going to write a tast class for your class under test (eg, CUT, ItemFactory) but your ItemFactory class is not compilable.

Will you fix your ItemFactory class and I will show you how to test it using PowerMockito with Mockito.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM