簡體   English   中英

如何模擬Couchbase Lite數據庫

[英]How to mock a Couchbase Lite database

我已經創建了一個服務層,該服務層使用DAO來獲取Couchbase Document對象並將其轉到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$2.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$1.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模擬,並測試是否返回相同的模擬? 例如:

Document doc1 = PowerMockito.mock(Document.class);
Document doc2 = PowerMockito.mock(Document.class);
when(listsDAO.findAll()).thenReturn(new Document[] {doc1, doc2});
...

嘗試為數據庫對象添加一個when語句,並返回該文檔的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);

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM