Skip to content

Instantly share code, notes, and snippets.

@RICH0423
Last active May 6, 2016 03:45
Show Gist options
  • Select an option

  • Save RICH0423/bc32f8ae36a25a6e79263cef272b422a to your computer and use it in GitHub Desktop.

Select an option

Save RICH0423/bc32f8ae36a25a6e79263cef272b422a to your computer and use it in GitHub Desktop.
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;
}
}
@RICH0423

RICH0423 commented May 6, 2016

Copy link
Copy Markdown
Author

Mockito and Powermock integration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment