Skip to content

Commit ddfde34

Browse files
authored
fixing robolectric unit tests, solves #467 (#468)
1 parent 48adac0 commit ddfde34

File tree

13 files changed

+56
-50
lines changed

13 files changed

+56
-50
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/.DS_Store
55
/build
66
*.iml
7-
.gradletasknamecache
7+
.gradletasknamecach
8+
*.exec

app-kotlin/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ dependencies {
5555
buildscript {
5656
repositories {
5757
mavenCentral()
58-
jcenter()
5958
google()
6059
maven {
6160
url 'https://plugins.gradle.org/m2/'

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ dependencies {
4242
buildscript {
4343
repositories {
4444
mavenCentral()
45-
jcenter()
4645
google()
4746
maven {
4847
url 'https://plugins.gradle.org/m2/'

build.gradle

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ apply plugin: 'io.codearte.nexus-staging'
55
ext {
66
minSdkVersion = 14
77
minSdkVersionApps = 23
8-
compileSdkVersion = 28
8+
compileSdkVersion = 29
99
buildToolsVersion = '28.0.3'
1010
gradleVersion = '4.6'
1111
kotlinVersion = '1.4.0'
1212
detektVersion = '1.0.0.RC6-1'
1313
}
1414

15-
ext.deps = [rxjava2 : 'io.reactivex.rxjava2:rxjava:2.2.19',
15+
ext.deps = [rxjava2 : 'io.reactivex.rxjava2:rxjava:2.2.21',
1616
rxandroid2 : 'io.reactivex.rxjava2:rxandroid:2.1.1',
1717
annotation : 'androidx.annotation:annotation:1.1.0',
1818
appcompat : 'androidx.appcompat:appcompat:1.2.0',
1919
junit : 'junit:junit:4.13',
2020
truth : 'com.google.truth:truth:1.0.1',
21-
robolectric : 'org.robolectric:robolectric:4.3.1',
21+
robolectric : 'org.robolectric:robolectric:4.9',
2222
mockitocore : 'org.mockito:mockito-core:3.5.2',
2323
nullaway : 'com.uber.nullaway:nullaway:0.8.0',
2424
errorprone : 'com.google.errorprone:error_prone_core:2.3.4',
@@ -31,14 +31,13 @@ ext.deps = [rxjava2 : 'io.reactivex.rxjava2:rxjava:2.2.19',
3131
buildscript {
3232
repositories {
3333
google()
34-
jcenter()
3534
mavenCentral()
3635
maven {
3736
url 'https://plugins.gradle.org/m2/'
3837
}
3938
}
4039
dependencies {
41-
classpath 'com.android.tools.build:gradle:3.6.3'
40+
classpath 'com.android.tools.build:gradle:3.6.4'
4241
classpath('com.hiya:jacoco-android:0.2') {
4342
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
4443
}
@@ -54,7 +53,6 @@ buildscript {
5453
allprojects {
5554
repositories {
5655
google()
57-
jcenter()
5856
mavenCentral()
5957
maven {
6058
url 'https://plugins.gradle.org/m2/'

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ org.gradle.jvmargs=-XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled -XX:+Heap
1818

1919
android.useAndroidX=true
2020
android.enableJetifier=true
21+
android.jetifier.blacklist=bcprov-jdk15on
2122
android.enableR8.fullMode=false
2223
android.enableUnitTestBinaryResources=true

library/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ android {
4949
jacoco {
5050
includeNoLocationClasses = true
5151
}
52+
systemProperty 'robolectric.dependency.repo.url', 'https://repo1.maven.org/maven2'
5253
}
5354
}
5455

@@ -65,6 +66,7 @@ android {
6566
api deps.rxandroid2
6667
implementation deps.annotation
6768

69+
testImplementation project(path: ':library')
6870
testImplementation deps.junit
6971
testImplementation deps.truth
7072
testImplementation deps.robolectric
@@ -92,5 +94,6 @@ android {
9294

9395
tasks.withType(Test) {
9496
jacoco.includeNoLocationClasses = true
97+
jacoco.excludes = ['jdk.internal.*']
9598
}
9699
}

library/src/test/java/com/github/pwittchen/reactivenetwork/library/rx2/ConnectivityTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
.typeName(TYPE_NAME_WIFI)
7474
.build();
7575

76+
//noinspection CStyleArrayDeclaration
7677
final NetworkInfo.State states[] =
7778
{ NetworkInfo.State.CONNECTED, NetworkInfo.State.CONNECTING };
7879

@@ -109,6 +110,7 @@
109110
.build();
110111

111112
// note that unknown type is added initially by the ConnectivityPredicate#hasType method
113+
//noinspection CStyleArrayDeclaration
112114
final int givenTypes[] = { connectivity.type(), Connectivity.UNKNOWN_TYPE };
113115

114116
// when
@@ -127,6 +129,7 @@
127129
.build();
128130

129131
// note that unknown type is added initially by the ConnectivityPredicate#hasType method
132+
//noinspection CStyleArrayDeclaration
130133
final int givenTypes[] = {
131134
ConnectivityManager.TYPE_WIFI, ConnectivityManager.TYPE_MOBILE, Connectivity.UNKNOWN_TYPE
132135
};
@@ -147,6 +150,7 @@
147150
.build();
148151

149152
// note that unknown type is added initially by the ConnectivityPredicate#hasType method
153+
//noinspection CStyleArrayDeclaration
150154
final int givenTypes[] = { ConnectivityManager.TYPE_MOBILE, Connectivity.UNKNOWN_TYPE };
151155

152156
// when
@@ -163,6 +167,7 @@ public void createShouldThrowAnExceptionWhenContextIsNull() {
163167
final Context context = null;
164168

165169
// when
170+
//noinspection ConstantConditions
166171
Connectivity.create(context);
167172

168173
// then
@@ -318,10 +323,11 @@ public void shouldAppendUnknownTypeWhileFilteringNetworkTypesInsidePredicateForE
318323

319324
@Test public void shouldCreateDefaultConnectivityWhenConnectivityManagerIsNull() {
320325
// given
321-
final Context context = RuntimeEnvironment.application.getApplicationContext();
326+
final Context context = RuntimeEnvironment.getApplication().getApplicationContext();
322327
final ConnectivityManager connectivityManager = null;
323328

324329
// when
330+
@SuppressWarnings("ConstantConditions")
325331
Connectivity connectivity = Connectivity.create(context, connectivityManager);
326332

327333
// then

library/src/test/java/com/github/pwittchen/reactivenetwork/library/rx2/ReactiveNetworkTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@
2929
import io.reactivex.Observable;
3030
import io.reactivex.Single;
3131
import java.lang.reflect.Method;
32-
import java.util.concurrent.Callable;
3332
import org.junit.Test;
3433
import org.junit.runner.RunWith;
3534
import org.robolectric.RobolectricTestRunner;
3635
import org.robolectric.RuntimeEnvironment;
3736
import org.robolectric.annotation.Config;
37+
import org.robolectric.shadows.ShadowConnectivityManager;
3838

3939
import static com.google.common.truth.Truth.assertThat;
4040

4141
@RunWith(RobolectricTestRunner.class)
42+
@Config(shadows = ShadowConnectivityManager.class)
4243
@SuppressWarnings("NullAway") public class ReactiveNetworkTest {
4344

4445
private static final String TEST_VALID_HOST = "www.test.com";
@@ -75,7 +76,11 @@
7576

7677
private void networkConnectivityObservableShouldNotBeNull() {
7778
// given
78-
final Context context = RuntimeEnvironment.application;
79+
final Application context = RuntimeEnvironment.getApplication();
80+
//final ConnectivityManager connectivityManagerMock = (ConnectivityManager) context
81+
// .getSystemService(Context.CONNECTIVITY_SERVICE);
82+
//
83+
//shadowOf(connectivityManagerMock);
7984

8085
// when
8186
Observable<Connectivity> observable;
@@ -87,7 +92,8 @@ private void networkConnectivityObservableShouldNotBeNull() {
8792

8893
@Test public void observeNetworkConnectivityWithStrategyShouldNotBeNull() {
8994
// given
90-
final Context context = RuntimeEnvironment.application;
95+
final Application context = RuntimeEnvironment.getApplication();
96+
9197
NetworkObservingStrategy strategy = new LollipopNetworkObservingStrategy();
9298

9399
// when
@@ -111,7 +117,7 @@ private void networkConnectivityObservableShouldNotBeNull() {
111117

112118
@Test public void observeNetworkConnectivityShouldBeConnectedOnStartWhenNetworkIsAvailable() {
113119
// given
114-
final Application context = RuntimeEnvironment.application;
120+
final Application context = RuntimeEnvironment.getApplication();
115121

116122
// when
117123
Connectivity connectivity = ReactiveNetwork.observeNetworkConnectivity(context).blockingFirst();
@@ -127,6 +133,7 @@ public void observeNetworkConnectivityShouldThrowAnExceptionForNullContext() {
127133
final NetworkObservingStrategy strategy = new LollipopNetworkObservingStrategy();
128134

129135
// when
136+
//noinspection ConstantConditions
130137
ReactiveNetwork.observeNetworkConnectivity(context, strategy);
131138

132139
// then an exception is thrown
@@ -151,6 +158,7 @@ public void observeInternetConnectivityShouldThrowAnExceptionWhenStrategyIsNull(
151158
final ErrorHandler errorHandler = new DefaultErrorHandler();
152159

153160
// when
161+
//noinspection ConstantConditions
154162
ReactiveNetwork.observeInternetConnectivity(strategy, TEST_VALID_INITIAL_INTERVAL,
155163
TEST_VALID_INTERVAL, TEST_VALID_HOST, TEST_VALID_PORT, TEST_VALID_TIMEOUT,
156164
TEST_VALID_HTTP_RESPONSE, errorHandler);
@@ -265,11 +273,7 @@ public void shouldCheckInternetConnectivityWithCustomSettings() {
265273

266274
@Override public Single<Boolean> checkInternetConnectivity(String host, int port,
267275
int timeoutInMs, int httpResponse, ErrorHandler errorHandler) {
268-
return Single.fromCallable(new Callable<Boolean>() {
269-
@Override public Boolean call() {
270-
return true;
271-
}
272-
});
276+
return Single.fromCallable(() -> true);
273277
}
274278

275279
@Override public String getDefaultPingHost() {
@@ -279,9 +283,7 @@ public void shouldCheckInternetConnectivityWithCustomSettings() {
279283
}
280284

281285
@NonNull private ErrorHandler createTestErrorHandler() {
282-
return new ErrorHandler() {
283-
@Override public void handleError(Exception exception, String message) {
284-
}
286+
return (exception, message) -> {
285287
};
286288
}
287289

library/src/test/java/com/github/pwittchen/reactivenetwork/library/rx2/internet/observing/InternetObservingSettingsTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ public void shouldBuildSettings() {
9191
}
9292

9393
@NonNull private ErrorHandler createTestErrorHandler() {
94-
return new ErrorHandler() {
95-
@Override public void handleError(Exception exception, String message) {
96-
}
97-
};
94+
return (exception, message) -> { };
9895
}
9996
}

library/src/test/java/com/github/pwittchen/reactivenetwork/library/rx2/network/observing/NetworkObservingStrategyTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
*/
1616
package com.github.pwittchen.reactivenetwork.library.rx2.network.observing;
1717

18+
import android.annotation.SuppressLint;
1819
import android.content.Context;
1920
import android.net.NetworkInfo;
2021
import com.github.pwittchen.reactivenetwork.library.rx2.Connectivity;
2122
import com.github.pwittchen.reactivenetwork.library.rx2.network.observing.strategy.LollipopNetworkObservingStrategy;
2223
import com.github.pwittchen.reactivenetwork.library.rx2.network.observing.strategy.PreLollipopNetworkObservingStrategy;
23-
import io.reactivex.functions.Consumer;
24+
import io.reactivex.disposables.Disposable;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627
import org.robolectric.RobolectricTestRunner;
2728
import org.robolectric.RuntimeEnvironment;
28-
2929
import static com.google.common.truth.Truth.assertThat;
3030

3131
@RunWith(RobolectricTestRunner.class)
@@ -47,17 +47,19 @@
4747
assertThatIsConnected(strategy);
4848
}
4949

50-
@SuppressWarnings("CheckReturnValue")
50+
@SuppressLint("CheckResult")
5151
private void assertThatIsConnected(NetworkObservingStrategy strategy) {
5252
// given
53-
final Context context = RuntimeEnvironment.application.getApplicationContext();
53+
final Context context = RuntimeEnvironment.getApplication().getApplicationContext();
5454

5555
//when
56-
strategy.observeNetworkConnectivity(context).subscribe(new Consumer<Connectivity>() {
57-
@Override public void accept(Connectivity connectivity) throws Exception {
58-
// then
59-
assertThat(connectivity.state()).isEqualTo(NetworkInfo.State.CONNECTED);
60-
}
61-
});
56+
Disposable disposable = strategy.observeNetworkConnectivity(context)
57+
.subscribe(
58+
(@SuppressLint("CheckResult") Connectivity connectivity) -> {
59+
// then
60+
assertThat(connectivity.state()).isEqualTo(NetworkInfo.State.CONNECTED);
61+
});
62+
63+
disposable.dispose();
6264
}
6365
}

0 commit comments

Comments
 (0)