如何模拟 Couchbase Lite 数据库
How to mock a Couchbase Lite database
我创建了一个服务层,它使用 DAO 获取 Couchbase 文档对象并将它们转换为 POJO。我想为此方法编写一个单元测试。
public List<Survey> findAll() throws CouchbaseLiteException {
List<Document> docList = listsDAO.findAll();
List<Survey> surveyList = new ArrayList<>();
for(Document doc: docList) {
ListClass listClass = listClassService.findById((String)doc.getProperty("listClass"));
Survey survey = new Survey.Builder().withName((String)doc.getProperty("name"))
.withListClass(listClass.getName()).build();
surveyList.add(survey);
}
return surveyList;
}
我已经尝试使用 PowerMockito
来做到这一点
@RunWith(PowerMockRunner.class)
@PrepareForTest({Database.class})
public class ListsServiceTest {
private Database database;
@Mock
private ListsDAO listsDAO;
@Mock
private ListClassService listClassService;
private ListsService listsService;
@Before
public void setup() {
database = PowerMockito.mock(Database.class);
listsService = new ListsService(listsDAO, listClassService);
}
@Test
public void testFindAll() {
try {
when(listsDAO.findAll()).thenReturn(TestUtils.createDocuments(database));
when(listClassService.findById("listClass_1")).thenReturn(TestUtils.LIST_CLASS1);
when(listClassService.findById("listClass_2")).thenReturn(TestUtils.LIST_CLASS2);
List<Survey> surveyList = listsService.findAll();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
}
但是我在使用 TestUtils.createDocuments()
方法时遇到了问题
private static Document createDocument(Database database, Map<String, Object> docMap) {
Document doc = database.createDocument();
try {
doc.putProperties(docMap);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
return doc;
}
public static List<Document> createDocuments(Database database) {
List<Document> docList = new ArrayList<>();
docList.add(createDocument(database, DOC_MAP1));
docList.add(createDocument(database, DOC_MAP2));
return docList;
}
我得到以下堆栈跟踪
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.xxx.xxx.service.ListsServiceTest.testFindAll(ListsServiceTest.java:51)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at com.xxx.xxx.util.TestUtils.createDocument(TestUtils.java:35)
at com.xxx.xxx.util.TestUtils.createDocuments(TestUtils.java:46)
at com.xxx.xxx.service.ListsServiceTest.testFindAll(ListsServiceTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code -1
我做错了什么?
TestUtils.createDocuments
尝试 运行 方法 database.createDocument()
,而在您的情况下 database
是模拟对象,而 createDocument
未被模拟。
我建议完全不要模拟数据库,并手动创建文档列表(无需调用 database.createDocument()
)。
如何创建一组文档模拟,并测试是否返回相同的模拟?例如:
Document doc1 = PowerMockito.mock(Document.class);
Document doc2 = PowerMockito.mock(Document.class);
when(listsDAO.findAll()).thenReturn(new Document[] {doc1, doc2});
...
尝试为数据库对象添加 when 语句,并 return 文档的 Mock 并在完成后验证方法调用。
// Add to your @Before method or in your test method
mockDocument = PowerMockito.mock(Document.class);
when(database.createDoccument()).thenReturn(mockDocument);
之后您可以验证是否将正确的参数传递给了您的模拟
Mockito.verify(mockDocument).putProperties(docMap);
希望对您有所帮助。
我创建了一个服务层,它使用 DAO 获取 Couchbase 文档对象并将它们转换为 POJO。我想为此方法编写一个单元测试。
public List<Survey> findAll() throws CouchbaseLiteException {
List<Document> docList = listsDAO.findAll();
List<Survey> surveyList = new ArrayList<>();
for(Document doc: docList) {
ListClass listClass = listClassService.findById((String)doc.getProperty("listClass"));
Survey survey = new Survey.Builder().withName((String)doc.getProperty("name"))
.withListClass(listClass.getName()).build();
surveyList.add(survey);
}
return surveyList;
}
我已经尝试使用 PowerMockito
来做到这一点
@RunWith(PowerMockRunner.class)
@PrepareForTest({Database.class})
public class ListsServiceTest {
private Database database;
@Mock
private ListsDAO listsDAO;
@Mock
private ListClassService listClassService;
private ListsService listsService;
@Before
public void setup() {
database = PowerMockito.mock(Database.class);
listsService = new ListsService(listsDAO, listClassService);
}
@Test
public void testFindAll() {
try {
when(listsDAO.findAll()).thenReturn(TestUtils.createDocuments(database));
when(listClassService.findById("listClass_1")).thenReturn(TestUtils.LIST_CLASS1);
when(listClassService.findById("listClass_2")).thenReturn(TestUtils.LIST_CLASS2);
List<Survey> surveyList = listsService.findAll();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
}
但是我在使用 TestUtils.createDocuments()
方法时遇到了问题
private static Document createDocument(Database database, Map<String, Object> docMap) {
Document doc = database.createDocument();
try {
doc.putProperties(docMap);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
return doc;
}
public static List<Document> createDocuments(Database database) {
List<Document> docList = new ArrayList<>();
docList.add(createDocument(database, DOC_MAP1));
docList.add(createDocument(database, DOC_MAP2));
return docList;
}
我得到以下堆栈跟踪
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.xxx.xxx.service.ListsServiceTest.testFindAll(ListsServiceTest.java:51)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at com.xxx.xxx.util.TestUtils.createDocument(TestUtils.java:35)
at com.xxx.xxx.util.TestUtils.createDocuments(TestUtils.java:46)
at com.xxx.xxx.service.ListsServiceTest.testFindAll(ListsServiceTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code -1
我做错了什么?
TestUtils.createDocuments
尝试 运行 方法 database.createDocument()
,而在您的情况下 database
是模拟对象,而 createDocument
未被模拟。
我建议完全不要模拟数据库,并手动创建文档列表(无需调用 database.createDocument()
)。
如何创建一组文档模拟,并测试是否返回相同的模拟?例如:
Document doc1 = PowerMockito.mock(Document.class);
Document doc2 = PowerMockito.mock(Document.class);
when(listsDAO.findAll()).thenReturn(new Document[] {doc1, doc2});
...
尝试为数据库对象添加 when 语句,并 return 文档的 Mock 并在完成后验证方法调用。
// Add to your @Before method or in your test method
mockDocument = PowerMockito.mock(Document.class);
when(database.createDoccument()).thenReturn(mockDocument);
之后您可以验证是否将正确的参数传递给了您的模拟
Mockito.verify(mockDocument).putProperties(docMap);
希望对您有所帮助。