Last active
May 6, 2016 03:45
-
-
Save RICH0423/bc32f8ae36a25a6e79263cef272b422a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.mockito.Mockito; | |
| import org.powermock.api.mockito.PowerMockito; | |
| import org.powermock.core.classloader.annotations.PrepareForTest; | |
| import org.powermock.modules.junit4.PowerMockRunner; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import static junit.framework.Assert.assertEquals; | |
| import static org.powermock.api.mockito.PowerMockito.when; | |
| @RunWith(PowerMockRunner.class) | |
| @PrepareForTest({Database.class, Document.class}) | |
| public class DBOperationManagementTest { | |
| private static final Map<String, Object> data = new HashMap<>(); | |
| private static final String EXPECT_DOC_ID = "id-1"; | |
| Database mockDatabase; | |
| Document mockDocument; | |
| @Before | |
| public void initMocks() throws CouchbaseLiteException { | |
| createMockObject(); | |
| } | |
| @After | |
| public void tearDown() throws Exception { | |
| resetMockObject(); | |
| } | |
| @Test | |
| public void test_createDoc(){ | |
| String docId = DBOperationManagement.createDoc(mockDatabase, data); | |
| System.out.println(docId); | |
| assertEquals(EXPECT_DOC_ID, docId); | |
| } | |
| @Test | |
| public void test_deleteDoc() throws CouchbaseLiteException { | |
| boolean deleteResult = DBOperationManagement.deleteDoc(mockDatabase, ""); | |
| assertEquals(true, deleteResult); | |
| Mockito.verify(mockDocument).delete(); | |
| } | |
| private void createMockObject() throws CouchbaseLiteException { | |
| mockDatabase = PowerMockito.mock(Database.class); | |
| mockDocument = PowerMockito.mock(Document.class); | |
| when(mockDocument.getId()).thenReturn("id-1"); | |
| when(mockDocument.putProperties(data)).thenReturn(null); | |
| when(mockDocument.isDeleted()).thenReturn(true); | |
| when(mockDatabase.createDocument()).thenReturn(mockDocument); | |
| when(mockDatabase.getDocument("")).thenReturn(mockDocument); | |
| } | |
| private void resetMockObject() { | |
| mockDatabase = null; | |
| mockDocument = null; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mockito and Powermock integration