Skip to content

Commit bcb3763

Browse files
committed
Convert unit test conditionals to @lazy properties
1 parent 08e1e7b commit bcb3763

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

android-junit5/src/test/groovy/de/mannodermaus/gradle/plugins/android_junit5/AndroidJUnitPlatformSpec.groovy

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ abstract class AndroidJUnitPlatformSpec extends Specification {
3333
/* SDK Directory, taken from the project itself and setup */
3434
static String sdkDir
3535

36+
@Lazy
37+
private static final boolean isAgp3x = {
38+
Version.ANDROID_GRADLE_PLUGIN_VERSION.startsWith("3.")
39+
}
40+
41+
@Lazy
42+
private static final boolean hasInternetAccess = {
43+
try {
44+
def url = new URL("https://www.google.com")
45+
def conn = url.openConnection()
46+
conn.connect()
47+
true
48+
49+
} catch (UnknownHostException ignored) {
50+
false
51+
}
52+
}
53+
3654
/* Per-test root project */
3755
Project testRoot
3856

@@ -52,20 +70,6 @@ abstract class AndroidJUnitPlatformSpec extends Specification {
5270
}
5371
}
5472

55-
private static boolean isAgp3x() {
56-
return Version.ANDROID_GRADLE_PLUGIN_VERSION.startsWith("3.")
57-
}
58-
59-
private static boolean hasInternetAccess() {
60-
try {
61-
// TODO Easier and/or more lightweight detection method?
62-
return InetAddress.getByName("https://google.com").isReachable(1000)
63-
64-
} catch (UnknownHostException ignored) {
65-
return false
66-
}
67-
}
68-
6973
/* Before Each */
7074

7175
def setup() {
@@ -144,7 +148,7 @@ abstract class AndroidJUnitPlatformSpec extends Specification {
144148
}
145149

146150
@SuppressWarnings("UnnecessaryQualifiedReference")
147-
@Requires({ AndroidJUnitPlatformSpec.hasInternetAccess() })
151+
@Requires({ AndroidJUnitPlatformSpec.hasInternetAccess })
148152
def "classpath assembled correctly"() {
149153
when:
150154
// Prepare another test project to link to
@@ -257,8 +261,8 @@ abstract class AndroidJUnitPlatformSpec extends Specification {
257261
}
258262

259263
@SuppressWarnings("UnnecessaryQualifiedReference")
260-
@IgnoreIf({ AndroidJUnitPlatformSpec.isAgp3x() })
261-
@Requires({ AndroidJUnitPlatformSpec.hasInternetAccess() })
264+
@Requires({ AndroidJUnitPlatformSpec.hasInternetAccess })
265+
@IgnoreIf({ AndroidJUnitPlatformSpec.isAgp3x })
262266
def "custom junit jupiter version"() {
263267
when:
264268
def nonExistentVersion = "0.0.0"

0 commit comments

Comments
 (0)