简体   繁体   English

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

[英]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. 在测试createBook()期间,我需要模拟的bsoInterface对象。 Can you please let me know how to do this? 你能让我知道怎么做吗?

Just create one more method that gets BSOInterfaceFactory as parameter. 只需再创建一个将BSOInterfaceFactory作为参数的方法即可。 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);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何模拟在仅使用模拟而不使用 powermockito 进行测试的方法中创建的对象? - How to mock an object which is created inside a method which is under test only with mockito not with powermockito? 如何在Mockito中模拟正在测试的班级中的私人班级成员 - How to mock private class members in a class under test with Mockito Mockito。 如何在被测类中模拟抽象方法调用? - Mockito. How to mock abstract method call in class under test? Mockito:模拟 object 不是成员,而是内联创建的 - Mockito: Mock object which is no member but is created inline Mockito:如何替换被测试类调用的类方法? - Mockito: How to replace method of class which is invoked by class under test? Mockito-所需的返回模拟对象未在被测类(CUT)中使用 - Mockito - Desired returned mock object not being used in class-under-test (CUT) 如何在Java中的被测类中注入被声明为private static的对象的模拟? - How to inject the mock of an object which is declared as private static in the class under test in java? 如何使用 Mockito 模拟由 spring 应用程序上下文创建的 object 以进行单元测试覆盖? - How to mock object created by spring application context using Mockito for Unit Test coverage? 如何使用 Mockito 在 class 中模拟 @injected object? - How to mock @injected object in class using Mockito? 如何告诉Mockito Mock注释返回一个非null的模拟对象? - How to tell a Mockito Mock annotation to return a mock object which is not null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM