Skip to content

Commit

Permalink
Avoid mocking java.lang.Runtime
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 725318466
  • Loading branch information
cushon authored and Google Java Core Libraries committed Feb 10, 2025
1 parent 5b19523 commit ccd06f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.verify;

import com.google.common.util.concurrent.UncaughtExceptionHandlers.Exiter;
import com.google.common.util.concurrent.UncaughtExceptionHandlers.RuntimeWrapper;
import junit.framework.TestCase;
import org.jspecify.annotations.NullUnmarked;

Expand All @@ -29,11 +30,11 @@
@NullUnmarked
public class UncaughtExceptionHandlersTest extends TestCase {

private Runtime runtimeMock;
private RuntimeWrapper runtimeMock;

@Override
protected void setUp() {
runtimeMock = mock(Runtime.class);
runtimeMock = mock(RuntimeWrapper.class);
}

public void testExiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ private UncaughtExceptionHandlers() {}
* process with an exit status of 1, indicating abnormal termination.
*/
public static UncaughtExceptionHandler systemExit() {
return new Exiter(Runtime.getRuntime());
return new Exiter(Runtime.getRuntime()::exit);
}

@VisibleForTesting
interface RuntimeWrapper {
void exit(int status);
}

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

private final Runtime runtime;
private final RuntimeWrapper runtime;

Exiter(Runtime runtime) {
Exiter(RuntimeWrapper runtime) {
this.runtime = runtime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.verify;

import com.google.common.util.concurrent.UncaughtExceptionHandlers.Exiter;
import com.google.common.util.concurrent.UncaughtExceptionHandlers.RuntimeWrapper;
import junit.framework.TestCase;
import org.jspecify.annotations.NullUnmarked;

Expand All @@ -29,11 +30,11 @@
@NullUnmarked
public class UncaughtExceptionHandlersTest extends TestCase {

private Runtime runtimeMock;
private RuntimeWrapper runtimeMock;

@Override
protected void setUp() {
runtimeMock = mock(Runtime.class);
runtimeMock = mock(RuntimeWrapper.class);
}

public void testExiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ private UncaughtExceptionHandlers() {}
* process with an exit status of 1, indicating abnormal termination.
*/
public static UncaughtExceptionHandler systemExit() {
return new Exiter(Runtime.getRuntime());
return new Exiter(Runtime.getRuntime()::exit);
}

@VisibleForTesting
interface RuntimeWrapper {
void exit(int status);
}

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

private final Runtime runtime;
private final RuntimeWrapper runtime;

Exiter(Runtime runtime) {
Exiter(RuntimeWrapper runtime) {
this.runtime = runtime;
}

Expand Down

0 comments on commit ccd06f2

Please sign in to comment.