Skip to content

Commit bd7be11

Browse files
authored
✨ Adds native android tests skeleton
1 parent b226de0 commit bd7be11

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

.circleci/config.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,27 @@ jobs:
9696

9797
- store_artifacts:
9898
path: InstabugSample/test-results
99-
99+
100+
test_android:
101+
working_directory: ~/project/InstabugSample
102+
docker:
103+
- image: circleci/android:api-28-node
104+
environment:
105+
JVM_OPTS: -Xmx3200m
106+
steps:
107+
- checkout:
108+
path: ~/project
109+
- run: yarn install
110+
- run:
111+
name: chmod permissions
112+
command: cd android && chmod +x ./gradlew
113+
- run:
114+
name: Download Dependencies
115+
command: cd android && ./gradlew androidDependencies
116+
- run:
117+
name: Run UnitTest
118+
command: cd android && ./gradlew test
119+
100120
publish:
101121
macos:
102122
xcode: "10.1.0"
@@ -115,10 +135,12 @@ workflows:
115135
- danger
116136
- test_module
117137
- test_sample
138+
- test_android
118139
- hold:
119140
requires:
120141
- test_module
121142
- test_sample
143+
- test_android
122144
type: approval
123145
filters:
124146
branches:

android/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ android {
2727

2828
dependencies {
2929
implementation 'com.facebook.react:react-native:+'
30-
api ('com.instabug.library:instabug:8.4.0.2'){
30+
api('com.instabug.library:instabug:8.4.0.2') {
3131
exclude group: 'com.android.support:appcompat-v7'
3232
}
33+
testImplementation 'org.mockito:mockito-core:1.10.19'
34+
testImplementation 'junit:junit:4.12'
35+
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
36+
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
37+
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.6'
38+
testImplementation 'org.powermock:powermock-module-junit4:1.6.6'
3339
}
3440

3541

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.instabug.reactlibrary;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
6+
import com.instabug.bug.BugReporting;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.mockito.Matchers;
12+
import org.mockito.Spy;
13+
import org.mockito.internal.verification.VerificationModeFactory;
14+
import org.mockito.invocation.InvocationOnMock;
15+
import org.mockito.stubbing.Answer;
16+
import org.powermock.api.mockito.PowerMockito;
17+
import org.powermock.core.classloader.annotations.PrepareForTest;
18+
import org.powermock.modules.junit4.PowerMockRunner;
19+
20+
import java.util.concurrent.Executors;
21+
import java.util.concurrent.ScheduledExecutorService;
22+
import java.util.concurrent.TimeUnit;
23+
24+
import static org.mockito.Matchers.any;
25+
import static org.mockito.Matchers.anyLong;
26+
import static org.powermock.api.mockito.PowerMockito.doAnswer;
27+
import static org.powermock.api.mockito.PowerMockito.mock;
28+
import static org.powermock.api.mockito.PowerMockito.when;
29+
30+
@RunWith(PowerMockRunner.class)
31+
@PrepareForTest({Looper.class, android.os.Handler.class, BugReporting.class, Runnable.class, RNInstabugReactnativeModule.class})
32+
33+
public class RNInstabugReactnativeModuleTest {
34+
35+
private RNInstabugReactnativeModule rnModule = new RNInstabugReactnativeModule(null,null,null);
36+
37+
private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor();
38+
39+
@Test
40+
public void testBugReportingInvoke() {
41+
PowerMockito.mockStatic(BugReporting.class);
42+
rnModule.invoke();
43+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
44+
BugReporting.invoke();
45+
}
46+
47+
@Before
48+
public void mockMainThreadHandler() throws Exception {
49+
PowerMockito.mockStatic(Looper.class);
50+
Looper mockMainThreadLooper = mock(Looper.class);
51+
when(Looper.getMainLooper()).thenReturn(mockMainThreadLooper);
52+
Handler mockMainThreadHandler = mock(Handler.class);
53+
Answer<Boolean> handlerPostAnswer = new Answer<Boolean>() {
54+
@Override
55+
public Boolean answer(InvocationOnMock invocation) throws Throwable {
56+
Runnable runnable = invocation.getArgumentAt(0, Runnable.class);
57+
Long delay = 0L;
58+
if (invocation.getArguments().length > 1) {
59+
delay = invocation.getArgumentAt(1, Long.class);
60+
}
61+
if (runnable != null) {
62+
mainThread.schedule(runnable, delay, TimeUnit.MILLISECONDS);
63+
}
64+
return true;
65+
}
66+
};
67+
doAnswer(handlerPostAnswer).when(mockMainThreadHandler).post(any(Runnable.class));
68+
doAnswer(handlerPostAnswer).when(mockMainThreadHandler).postDelayed(any(Runnable.class), anyLong());
69+
PowerMockito.whenNew(Handler.class).withArguments(mockMainThreadLooper).thenReturn(mockMainThreadHandler);
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)