Skip to content

Commit 87e9706

Browse files
committed
Swap out mocks for simple fakes.
1 parent 1a145db commit 87e9706

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed

rxandroid/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ dependencies {
4343
compile 'io.reactivex:rxjava:1.1.6'
4444

4545
testCompile 'junit:junit:4.12'
46-
testCompile 'org.mockito:mockito-core:1.10.19'
4746
testCompile 'org.robolectric:robolectric:3.0'
4847
}
4948

rxandroid/src/test/java/rx/android/schedulers/AndroidSchedulersTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
import org.junit.After;
1717
import org.junit.Before;
1818
import org.junit.Test;
19-
2019
import rx.Scheduler;
2120
import rx.android.plugins.RxAndroidPlugins;
2221
import rx.android.plugins.RxAndroidSchedulersHook;
22+
import rx.android.testutil.EmptyScheduler;
2323

2424
import static org.junit.Assert.assertSame;
25-
import static org.mockito.Mockito.mock;
2625

2726
public class AndroidSchedulersTest {
2827

@@ -33,7 +32,7 @@ public void setUpAndTearDown() {
3332

3433
@Test
3534
public void mainThreadCallsThroughToHook() {
36-
final Scheduler scheduler = mock(Scheduler.class);
35+
final Scheduler scheduler = new EmptyScheduler();
3736
RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
3837
@Override public Scheduler getMainThreadScheduler() {
3938
return scheduler;

rxandroid/src/test/java/rx/android/schedulers/LooperSchedulerTest.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import rx.Subscription;
2828
import rx.android.plugins.RxAndroidPlugins;
2929
import rx.android.plugins.RxAndroidSchedulersHook;
30+
import rx.android.testutil.CountingAction;
3031
import rx.exceptions.OnErrorNotImplementedException;
3132
import rx.functions.Action0;
3233

@@ -36,9 +37,6 @@
3637
import static org.junit.Assert.assertSame;
3738
import static org.junit.Assert.assertTrue;
3839
import static org.junit.Assert.fail;
39-
import static org.mockito.Mockito.mock;
40-
import static org.mockito.Mockito.never;
41-
import static org.mockito.Mockito.verify;
4240
import static org.robolectric.shadows.ShadowLooper.idleMainLooper;
4341
import static org.robolectric.shadows.ShadowLooper.pauseMainLooper;
4442
import static org.robolectric.shadows.ShadowLooper.runUiThreadTasks;
@@ -67,57 +65,57 @@ public void tearDown() {
6765
public void schedulePostsActionImmediately() {
6866
Worker worker = scheduler.createWorker();
6967

70-
Action0 action = mock(Action0.class);
68+
CountingAction action = new CountingAction();
7169
worker.schedule(action);
7270

7371
runUiThreadTasks();
74-
verify(action).call();
72+
assertEquals(1, action.get());
7573
}
7674

7775
@Test
7876
public void scheduleWithDelayPostsActionWithDelay() {
7977
Worker worker = scheduler.createWorker();
8078

81-
Action0 action = mock(Action0.class);
79+
CountingAction action = new CountingAction();
8280
worker.schedule(action, 1, MINUTES);
8381

8482
runUiThreadTasks();
85-
verify(action, never()).call();
83+
assertEquals(0, action.get());
8684

8785
idleMainLooper(MINUTES.toMillis(1));
8886
runUiThreadTasks();
89-
verify(action).call();
87+
assertEquals(1, action.get());
9088
}
9189

9290
@Test
9391
public void unsubscribeCancelsScheduledAction() {
9492
Worker worker = scheduler.createWorker();
9593

96-
Action0 action = mock(Action0.class);
94+
CountingAction action = new CountingAction();
9795
Subscription subscription = worker.schedule(action);
9896
subscription.unsubscribe();
9997

10098
runUiThreadTasks();
101-
verify(action, never()).call();
99+
assertEquals(0, action.get());
102100
}
103101

104102
@Test
105103
public void unsubscribeCancelsScheduledActionWithDelay() {
106104
Worker worker = scheduler.createWorker();
107105

108-
Action0 action = mock(Action0.class);
106+
CountingAction action = new CountingAction();
109107
Subscription subscription = worker.schedule(action, 1, MINUTES);
110108
subscription.unsubscribe();
111109

112110
runUiThreadTasksIncludingDelayedTasks();
113-
verify(action, never()).call();
111+
assertEquals(0, action.get());
114112
}
115113

116114
@Test
117115
public void unsubscribeState() {
118116
Worker worker = scheduler.createWorker();
119117

120-
Action0 action = mock(Action0.class);
118+
Action0 action = new CountingAction();
121119
Subscription subscription = worker.schedule(action);
122120
assertFalse(subscription.isUnsubscribed());
123121

@@ -127,7 +125,7 @@ public void unsubscribeState() {
127125

128126
@Test
129127
public void schedulerHookIsUsed() {
130-
final Action0 newAction = mock(Action0.class);
128+
final CountingAction newAction = new CountingAction();
131129
final AtomicReference<Action0> actionRef = new AtomicReference<>();
132130
RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
133131
@Override public Action0 onSchedule(Action0 action) {
@@ -138,28 +136,28 @@ public void schedulerHookIsUsed() {
138136

139137
Worker worker = scheduler.createWorker();
140138

141-
Action0 action = mock(Action0.class);
139+
CountingAction action = new CountingAction();
142140
worker.schedule(action);
143141

144142
// Verify our action was passed to the schedulers hook.
145143
assertSame(action, actionRef.get());
146144

147145
// Verify the scheduled action was the one returned from the hook.
148146
runUiThreadTasks();
149-
verify(newAction).call();
150-
verify(action, never()).call();
147+
assertEquals(1, newAction.get());
148+
assertEquals(0, action.get());
151149
}
152150

153151
@Test
154152
public void workerUnsubscriptionPreventsScheduling() {
155153
Worker worker = scheduler.createWorker();
156154
worker.unsubscribe();
157155

158-
Action0 action = mock(Action0.class);
156+
CountingAction action = new CountingAction();
159157
worker.schedule(action);
160158

161159
runUiThreadTasks();
162-
verify(action, never()).call();
160+
assertEquals(0, action.get());
163161
}
164162

165163
@Test
@@ -176,41 +174,41 @@ public void workerUnsubscriptionDuringSchedulingCancelsScheduledAction() {
176174
Scheduler.Worker worker = scheduler.createWorker();
177175
workerRef.set(worker);
178176

179-
Action0 action = mock(Action0.class);
177+
CountingAction action = new CountingAction();
180178
worker.schedule(action);
181179

182180
runUiThreadTasks();
183-
verify(action, never()).call();
181+
assertEquals(0, action.get());
184182
}
185183

186184
@Test
187185
public void workerUnsubscriptionCancelsScheduled() {
188186
Worker worker = scheduler.createWorker();
189187

190-
Action0 action = mock(Action0.class);
188+
CountingAction action = new CountingAction();
191189
worker.schedule(action, 1, MINUTES);
192190

193191
worker.unsubscribe();
194192

195193
runUiThreadTasks();
196-
verify(action, never()).call();
194+
assertEquals(0, action.get());
197195
}
198196

199197
@Test
200198
public void workerUnsubscriptionDoesNotAffectOtherWorkers() {
201199
Scheduler.Worker workerA = scheduler.createWorker();
202-
Action0 actionA = mock(Action0.class);
200+
CountingAction actionA = new CountingAction();
203201
workerA.schedule(actionA, 1, MINUTES);
204202

205203
Scheduler.Worker workerB = scheduler.createWorker();
206-
Action0 actionB = mock(Action0.class);
204+
CountingAction actionB = new CountingAction();
207205
workerB.schedule(actionB, 1, MINUTES);
208206

209207
workerA.unsubscribe();
210208

211209
runUiThreadTasksIncludingDelayedTasks();
212-
verify(actionA, never()).call();
213-
verify(actionB).call();
210+
assertEquals(0, actionA.get());
211+
assertEquals(1, actionB.get());
214212
}
215213

216214
@Test
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package rx.android.testutil;
15+
16+
import java.util.concurrent.atomic.AtomicInteger;
17+
import rx.functions.Action0;
18+
19+
public final class CountingAction extends AtomicInteger implements Action0 {
20+
@Override
21+
public void call() {
22+
getAndIncrement();
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package rx.android.testutil;
15+
16+
import rx.Scheduler;
17+
18+
public final class EmptyScheduler extends Scheduler {
19+
@Override
20+
public Worker createWorker() {
21+
throw new UnsupportedOperationException();
22+
}
23+
}

0 commit comments

Comments
 (0)