作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要模拟一个从被测方法实例化的对象。请检查以下代码以供引用。
被测类:
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 对象。你能告诉我该怎么做吗?
最佳答案
只需再创建一个获取 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);
}
关于java - 如何在 Mockito 中模拟在被测类中创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916403/
我是一名优秀的程序员,十分优秀!