如何在Mockito中模拟在被测试的类中创建的对象

时间:2015-06-18 13:15:42

标签: java unit-testing

我需要模拟一个从被测方法实例化的对象。请查看以下代码供您参考。

待测班级:

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);
    }
}

这是测试类:

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");
    }

}

在测试createBook()期间,我需要我需要模拟的bsoInterface对象。你能告诉我怎么做吗?

1 个答案:

答案 0 :(得分:1)

再创建一个将BSOInterfaceFactory作为参数的方法。您可以在测试中使用第二个。

public void createBook(){
    create(new BSOInterfaceFactory());
}

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

并且测试应该如下:

  @Test
  public void testCreateBook(){
        BSOInterfaceFactory bsoInterfaceFactory = mock(BSOInterfaceFactory.class);
        b.createBook(bsoInterfaceFactory);
  }