Q:

How to Mock an object which is created in the class under test in Mockito

I need to mock an object which is instantiated from the method under test.  Please check the below code for your reference.
Class Under Test:
package org.sambaran.model; 
import java.util.List;  
public class Book implements Item {
    private String isbn;
    private String title;
    private String description;
    private List<Author> authors;
    private BSOInterface bsoInterface;
    public Book(String isbn, String title, String description,
            List<Author> authors) {
        super();
        this.isbn = isbn;
        this.title = title;
        this.description = description;
        this.authors = authors;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }

    public void createBook(){
        bsoInterface=new BSOInterfaceFactory().getBSOInterface();
        bsoInterface.createBook(this);
    }
}

This is the test class:
package org.sambaran.model.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.sambaran.model.Author;
import org.sambaran.model.BSOInterface;
import org.sambaran.model.Book;
import org.sambaran.model.Item;

public class BookTest {
    @Mock
    BSOInterface bsoInterface;
    Item b;
    @Before
    public void setUp() throws Exception {

        b=new Book("123-654-6789", "Head First C", "First Book on C", new ArrayList<Author>());
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetIsbn() {
        assertNotNull(b.getIsbn());

    }

    @Test
    public void testSetIsbn() {
        String isbn="111-222-3333";
        b.setIsbn(isbn);
        assertEquals(b.getIsbn(), isbn);
    }

    @Test
    public void testGetTitle() {
        Book book=Mockito.mock(Book.class);
        assertNotNull(book.getDescription());
    }

    @Test
    public void testCreateBook(){

        /**
         * Here I need to mock the bsoInterface but the object is created in this method only.
         */
        b.createBook();
    }
    @Test
    public void testSetTitle() {
        fail("Not yet implemented");
    }

    @Test
    public void testGetDescription() {
        fail("Not yet implemented");
    }

    @Test
    public void testSetDescription() {
        fail("Not yet implemented");
    }

    @Test
    public void testGetAuthors() {
        fail("Not yet implemented");
    }

    @Test
    public void testSetAuthors() {
        fail("Not yet implemented");
    }

}

During testing createBook() I need the bsoInterface object which I need to mock. Can you please let me know how to do this?

A:

Just create one more method that gets BSOInterfaceFactory as parameter. You can use the second one in tests.
public void createBook(){
    create(new BSOInterfaceFactory());
}

public void create(BSOInterfaceFactory bsoInterfacefactory) {
    this.bsoInterface=bsoInterfacefactory.getBSOInterface();
    bsoInterface.createBook(this);
}

and the test should look like:
  @Test
  public void testCreateBook(){
        BSOInterfaceFactory bsoInterfaceFactory = mock(BSOInterfaceFactory.class);
        b.createBook(bsoInterfaceFactory);
  }

