Skip to content

Commit

Permalink
fix: add test for dbutil
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Aug 22, 2024
1 parent b5c8edb commit d43e628
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package software.aws.solution.clickstream.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;

Expand All @@ -28,13 +30,19 @@
import org.robolectric.annotation.Config;
import software.aws.solution.clickstream.AnalyticsEventTest;
import software.aws.solution.clickstream.client.AnalyticsEvent;
import software.aws.solution.clickstream.client.db.ClickstreamDBBase;
import software.aws.solution.clickstream.client.db.ClickstreamDBUtil;
import software.aws.solution.clickstream.util.ReflectUtil;

import java.util.Objects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
Expand Down Expand Up @@ -63,6 +71,21 @@ public void testInsertSingleEvent() {
assertNotEquals(idInserted, 0);
}

/**
* test insert single event failed.
*
* @throws Exception exception.
*/
@Test
public void testInsertSingleEventFailed() throws Exception {
ClickstreamDBBase clickstreamDBBase = mock(ClickstreamDBBase.class);
ReflectUtil.modifyFiled(dbUtil, "clickstreamDBBase", clickstreamDBBase);
doThrow(new SQLException("Mocked SQLException")).when(clickstreamDBBase).insert(any(Uri.class), any(
ContentValues.class));
Uri uri = dbUtil.saveEvent(analyticsEvent);
assertNull(uri);
}

/**
* test query all.
*/
Expand Down

0 comments on commit d43e628

Please sign in to comment.