我们都编写了单元测试,但是有时我们面临的挑战是被测单元可能依赖于其他组件。 并且配置其他组件进行单元测试绝对是一个过大的选择。 相反,我们可以使用Mocks代替其他组件,并继续进行单元测试。
为了说明如何使用模拟,我有一个数据访问层(DAL),基本上是一个类,为应用程序提供API,以供应用程序访问和修改数据存储库中的数据。 然后,我对DAL进行单元测试,而实际上无需连接到数据存储库。 数据存储库可以是本地数据库或远程数据库,也可以是文件系统,也可以是我们可以存储和检索数据的任何位置。 DAL类的使用有助于我们将数据映射器与应用程序代码分开。
让我们使用maven创建一个Java项目。
mvn archetype:generate -DgroupId=info.sanaulla -DartifactId=MockitoDemo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
上面创建了一个文件夹MockitoDemo,然后为源文件和测试文件创建了整个目录结构。
考虑此示例的以下模型类:
package info.sanaulla.models;import java.util.List;/**
* Model class for the book details.
*/
public class Book {private String isbn;private String title;private List<String> authors;private String publication;private Integer yearOfPublication;private Integer numberOfPages;private String image;public Book(String isbn,String title,List<String> authors,String publication,Integer yearOfPublication,Integer numberOfPages,String image){this.isbn = isbn;this.title = title;this.authors = authors;this.publication = publication;this.yearOfPublication = yearOfPublication;this.numberOfPages = numberOfPages;this.image = image;}public String getIsbn() {return isbn;}public String getTitle() {return title;}public List<String> getAuthors() {return authors;}public String getPublication() {return publication;}public Integer getYearOfPublication() {return yearOfPublication;}public Integer getNumberOfPages() {return numberOfPages;}public String getImage() {return image;}
}
用于对Book模型类进行操作的DAL类为:
package info.sanaulla.dal;import info.sanaulla.models.Book;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;/**
* API layer for persisting and retrieving the Book objects.
*/
public class BookDAL {private static BookDAL bookDAL = new BookDAL();public List<Book> getAllBooks(){return Collections.EMPTY_LIST;}public Book getBook(String isbn){return null;}public String addBook(Book book){return book.getIsbn();}public String updateBook(Book book){return book.getIsbn();}public static BookDAL getInstance(){return bookDAL;}
}
上面的DAL层目前没有功能,我们将对该单元代码( TDD )进行单元测试。 DAL层可能与我们在设计API时不关心的ORM映射器或数据库API通信。
测试DAL层
Java中有许多用于单元测试和模拟的框架,但是在这个示例中,我将选择JUnit用于单元测试,选择Mockito用于模拟 。 我们将不得不更新Maven的pom.xml中的依赖项
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>info.sanaulla</groupId><artifactId>MockitoDemo</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>MockitoDemo</name><url>http://maven.apache.org</url><dependencies><!-- Dependency for JUnit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><!-- Dependency for Mockito --><dependency><groupId>org.mockito</groupId><artifactId>mockito-all</artifactId><version>1.9.5</version><scope>test</scope></dependency></dependencies>
</project>
现在让单元测试BookDAL
。 在单元测试期间,我们会将模拟数据注入BookDAL中,以便我们无需依赖数据源即可完成API的测试。
最初,我们将有一个空的测试类:
public class BookDALTest {public void setUp() throws Exception {}public void testGetAllBooks() throws Exception {}public void testGetBook() throws Exception {}public void testAddBook() throws Exception {}public void testUpdateBook() throws Exception {}
}
我们将把模拟的BookDAL
和模拟的数据注入到setUp()
,如下所示:
public class BookDALTest {private static BookDAL mockedBookDAL;private static Book book1;private static Book book2;@BeforeClasspublic static void setUp(){//Create mock object of BookDALmockedBookDAL = mock(BookDAL.class);//Create few instances of Book class.book1 = new Book("8131721019","Compilers Principles",Arrays.asList("D. Jeffrey Ulman","Ravi Sethi", "Alfred V. Aho", "Monica S. Lam"),"Pearson Education Singapore Pte Ltd", 2008,1009,"BOOK_IMAGE");book2 = new Book("9788183331630","Let Us C 13th Edition",Arrays.asList("Yashavant Kanetkar"),"BPB PUBLICATIONS", 2012,675,"BOOK_IMAGE");//Stubbing the methods of mocked BookDAL with mocked data. when(mockedBookDAL.getAllBooks()).thenReturn(Arrays.asList(book1, book2));when(mockedBookDAL.getBook("8131721019")).thenReturn(book1);when(mockedBookDAL.addBook(book1)).thenReturn(book1.getIsbn());when(mockedBookDAL.updateBook(book1)).thenReturn(book1.getIsbn());}public void testGetAllBooks() throws Exception {}public void testGetBook() throws Exception {}public void testAddBook() throws Exception {}public void testUpdateBook() throws Exception {}
}
在上面的setUp()
方法中,我有:
- 创建了BookDAL的模拟对象
BookDAL mockedBookDAL = mock(BookDAL.class);
- 在BookDAL的API中添加模拟数据,以便在调用API时返回模拟数据。
//When getAllBooks() is invoked then return the given data and so on for the other methods. when(mockedBookDAL.getAllBooks()).thenReturn(Arrays.asList(book1, book2)); when(mockedBookDAL.getBook("8131721019")).thenReturn(book1); when(mockedBookDAL.addBook(book1)).thenReturn(book1.getIsbn()); when(mockedBookDAL.updateBook(book1)).thenReturn(book1.getIsbn());
填充其余的测试,我们得到:
package info.sanaulla.dal;import info.sanaulla.models.Book;
import org.junit.BeforeClass;
import org.junit.Test;import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;import java.util.Arrays;
import java.util.List;public class BookDALTest {private static BookDAL mockedBookDAL;private static Book book1;private static Book book2;@BeforeClasspublic static void setUp(){mockedBookDAL = mock(BookDAL.class);book1 = new Book("8131721019","Compilers Principles",Arrays.asList("D. Jeffrey Ulman","Ravi Sethi", "Alfred V. Aho", "Monica S. Lam"),"Pearson Education Singapore Pte Ltd", 2008,1009,"BOOK_IMAGE");book2 = new Book("9788183331630","Let Us C 13th Edition",Arrays.asList("Yashavant Kanetkar"),"BPB PUBLICATIONS", 2012,675,"BOOK_IMAGE");when(mockedBookDAL.getAllBooks()).thenReturn(Arrays.asList(book1, book2));when(mockedBookDAL.getBook("8131721019")).thenReturn(book1);when(mockedBookDAL.addBook(book1)).thenReturn(book1.getIsbn());when(mockedBookDAL.updateBook(book1)).thenReturn(book1.getIsbn());}@Testpublic void testGetAllBooks() throws Exception {List<Book> allBooks = mockedBookDAL.getAllBooks();assertEquals(2, allBooks.size());Book myBook = allBooks.get(0);assertEquals("8131721019", myBook.getIsbn());assertEquals("Compilers Principles", myBook.getTitle());assertEquals(4, myBook.getAuthors().size());assertEquals((Integer)2008, myBook.getYearOfPublication());assertEquals((Integer) 1009, myBook.getNumberOfPages());assertEquals("Pearson Education Singapore Pte Ltd", myBook.getPublication());assertEquals("BOOK_IMAGE", myBook.getImage());}@Testpublic void testGetBook(){String isbn = "8131721019";Book myBook = mockedBookDAL.getBook(isbn);assertNotNull(myBook);assertEquals(isbn, myBook.getIsbn());assertEquals("Compilers Principles", myBook.getTitle());assertEquals(4, myBook.getAuthors().size());assertEquals("Pearson Education Singapore Pte Ltd", myBook.getPublication());assertEquals((Integer)2008, myBook.getYearOfPublication());assertEquals((Integer)1009, myBook.getNumberOfPages());}@Testpublic void testAddBook(){String isbn = mockedBookDAL.addBook(book1);assertNotNull(isbn);assertEquals(book1.getIsbn(), isbn);}@Testpublic void testUpdateBook(){String isbn = mockedBookDAL.updateBook(book1);assertNotNull(isbn);assertEquals(book1.getIsbn(), isbn);}
}
可以使用maven命令运行测试: mvn test
。 输出为:
-------------------------------------------------------T E S T S
-------------------------------------------------------
Running info.sanaulla.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 sec
Running info.sanaulla.dal.BookDALTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.209 secResults :Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
因此,我们已经能够测试DAL类,而无需使用模拟实际配置数据源。
翻译自: https://www.javacodegeeks.com/2014/02/getting-started-with-mocking-in-java-using-mockito.html