[英]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.