Skip to content

Commit ccd06f2

Browse files
cushonGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Avoid mocking java.lang.Runtime
RELNOTES=n/a PiperOrigin-RevId: 725318466
1 parent 5b19523 commit ccd06f2

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

android/guava-tests/test/com/google/common/util/concurrent/UncaughtExceptionHandlersTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.Mockito.verify;
2121

2222
import com.google.common.util.concurrent.UncaughtExceptionHandlers.Exiter;
23+
import com.google.common.util.concurrent.UncaughtExceptionHandlers.RuntimeWrapper;
2324
import junit.framework.TestCase;
2425
import org.jspecify.annotations.NullUnmarked;
2526

@@ -29,11 +30,11 @@
2930
@NullUnmarked
3031
public class UncaughtExceptionHandlersTest extends TestCase {
3132

32-
private Runtime runtimeMock;
33+
private RuntimeWrapper runtimeMock;
3334

3435
@Override
3536
protected void setUp() {
36-
runtimeMock = mock(Runtime.class);
37+
runtimeMock = mock(RuntimeWrapper.class);
3738
}
3839

3940
public void testExiter() {

android/guava/src/com/google/common/util/concurrent/UncaughtExceptionHandlers.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,21 @@ private UncaughtExceptionHandlers() {}
5050
* process with an exit status of 1, indicating abnormal termination.
5151
*/
5252
public static UncaughtExceptionHandler systemExit() {
53-
return new Exiter(Runtime.getRuntime());
53+
return new Exiter(Runtime.getRuntime()::exit);
54+
}
55+
56+
@VisibleForTesting
57+
interface RuntimeWrapper {
58+
void exit(int status);
5459
}
5560

5661
@VisibleForTesting
5762
static final class Exiter implements UncaughtExceptionHandler {
5863
private static final LazyLogger logger = new LazyLogger(Exiter.class);
5964

60-
private final Runtime runtime;
65+
private final RuntimeWrapper runtime;
6166

62-
Exiter(Runtime runtime) {
67+
Exiter(RuntimeWrapper runtime) {
6368
this.runtime = runtime;
6469
}
6570

guava-tests/test/com/google/common/util/concurrent/UncaughtExceptionHandlersTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.Mockito.verify;
2121

2222
import com.google.common.util.concurrent.UncaughtExceptionHandlers.Exiter;
23+
import com.google.common.util.concurrent.UncaughtExceptionHandlers.RuntimeWrapper;
2324
import junit.framework.TestCase;
2425
import org.jspecify.annotations.NullUnmarked;
2526

@@ -29,11 +30,11 @@
2930
@NullUnmarked
3031
public class UncaughtExceptionHandlersTest extends TestCase {
3132

32-
private Runtime runtimeMock;
33+
private RuntimeWrapper runtimeMock;
3334

3435
@Override
3536
protected void setUp() {
36-
runtimeMock = mock(Runtime.class);
37+
runtimeMock = mock(RuntimeWrapper.class);
3738
}
3839

3940
public void testExiter() {

guava/src/com/google/common/util/concurrent/UncaughtExceptionHandlers.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,21 @@ private UncaughtExceptionHandlers() {}
5050
* process with an exit status of 1, indicating abnormal termination.
5151
*/
5252
public static UncaughtExceptionHandler systemExit() {
53-
return new Exiter(Runtime.getRuntime());
53+
return new Exiter(Runtime.getRuntime()::exit);
54+
}
55+
56+
@VisibleForTesting
57+
interface RuntimeWrapper {
58+
void exit(int status);
5459
}
5560

5661
@VisibleForTesting
5762
static final class Exiter implements UncaughtExceptionHandler {
5863
private static final LazyLogger logger = new LazyLogger(Exiter.class);
5964

60-
private final Runtime runtime;
65+
private final RuntimeWrapper runtime;
6166

62-
Exiter(Runtime runtime) {
67+
Exiter(RuntimeWrapper runtime) {
6368
this.runtime = runtime;
6469
}
6570

0 commit comments

Comments
 (0)