我如何正确测试这种方法,我的测试是否可以告诉我它是否可以?
public List<Book> findByCategory(String category){
List<Book> bookList = bookRepository.findByCategory(category);
List<Book> activeBookList = new ArrayList<>();
for (Book book: bookList) {
if(book.isActive()) {
activeBookList.add(book);
}
}
return activeBookList;
}
这是测试
@Test
public void findByCategoryTest() throws Exception {
List<Book> bookList = new ArrayList<>();
expect(bookRepository.findByCategory(anyObject())).andReturn(bookList);
expect(bookRepository.findByCategory(anyObject())).andReturn(bookList);
Assert.assertNotNull(bookList);
}
答案 0 :(得分:1)
首先,这看起来不像是对存储库的测试,而是对注入了存储库的服务的测试。那将是一个像这样的BookRepository的BookService。
public class BookService {
public BookRepository bookRepository;
public List<Book> findByCategory(String category) {
List<Book> bookList = bookRepository.findByCategory(category);
List<Book> activeBookList = new ArrayList<>();
for (Book book : bookList) {
if (book.isActive()) {
activeBookList.add(book);
}
}
return activeBookList;
}
public void setBookRepository(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
}
或者最好使用构造函数注入。
public class BookService {
public BookRepository bookRepository;
public BookService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public List<Book> findByCategory(String category) {
List<Book> bookList = bookRepository.findByCategory(category);
List<Book> activeBookList = new ArrayList<>();
for (Book book : bookList) {
if (book.isActive()) {
activeBookList.add(book);
}
}
return activeBookList;
}
}
通过查看您的代码,我假设您使用的是EasyMock(因为您使用的是expect和andReturn),这是您未在问题中指定的内容。 EasyMock不像Mockito那样容易支持构造函数注入(它确实如此,但仅在最近的版本中),所以你可能需要setter。
现在,使用Mockito,您可以轻松地模拟存储库并添加您的验证和断言。
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class BookServiceTest {
@InjectMocks
private BookService bookService;
@Mock
private BookRepository bookRepository;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void findByCategoryTest() throws Exception {
Book book1 = new Book();
book1.setActive(true);
Book book2 = new Book();
book2.setActive(false);
when(bookRepository.findByCategory(anyString()))
.thenReturn(Arrays.asList(book1, book2));
List<Book> bookList = bookService.findByCategory("category1");
verify(bookRepository).findByCategory(anyString());
assertNotNull(bookList);
assertEquals(1, bookList.size());
assertEquals(book1, bookList.get(0));
}
}
使用EasyMock,就是这样。
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.easymock.EasyMockRunner;
import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(EasyMockRunner.class)
public class BookServiceTest2 {
private EasyMockSupport support = new EasyMockSupport();
@TestSubject
private BookService bookService = new BookService();
@Mock
private BookRepository bookRepository;
@Test
public void findByCategoryTest() throws Exception {
Book book1 = new Book();
book1.setActive(true);
Book book2 = new Book();
book2.setActive(false);
expect(bookRepository.findByCategory(anyString())).andReturn(Arrays.asList(book1, book2));
replay(bookRepository);
List<Book> bookList = bookService.findByCategory("category1");
support.verifyAll();
assertNotNull(bookList);
assertEquals(1, bookList.size());
assertEquals(book1, bookList.get(0));
}
}
正如您所看到的,您没有在示例中测试任何内容,因为您没有调用实际代码,而只是设置了模拟的期望。
希望有所帮助。