Skip to content

Commit 74f47da

Browse files
committed
F no checked exception when using SourceDirectoryFinder
1 parent b02e6d2 commit 74f47da

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

approvaltests-util/src/main/java/com/spun/util/tests/TestUtils.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,26 @@ public class TestUtils
2828
{
2929
private static Random random;
3030
private static Function2<Class, String, File> getSourceDirectory = ClassUtils::getSourceDirectory;
31-
public static AutoCloseable registerSourceDirectoryFinder(Function2<Class, String, File> sourceDirectoryFinder)
31+
public static SourceDirectoryRestorer registerSourceDirectoryFinder(
32+
Function2<Class, String, File> sourceDirectoryFinder)
3233
{
33-
Function2<Class, String, File> original = getSourceDirectory;
34-
AutoCloseable c = () -> TestUtils.getSourceDirectory = original;
34+
SourceDirectoryRestorer c = new SourceDirectoryRestorer();
3535
TestUtils.getSourceDirectory = sourceDirectoryFinder;
3636
return c;
3737
}
38+
public static class SourceDirectoryRestorer implements AutoCloseable
39+
{
40+
private final Function2<Class, String, File> original;
41+
public SourceDirectoryRestorer()
42+
{
43+
this.original = TestUtils.getSourceDirectory;
44+
}
45+
@Override
46+
public void close()
47+
{
48+
TestUtils.getSourceDirectory = original;
49+
}
50+
}
3851
public static File getFile(String startingDir)
3952
{
4053
JFrame frame = new JFrame();

approvaltests/src/test/java/org/approvaltests/namer/JUnit5StackTraceNamerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.File;
44

5-
import org.junit.Assert;
65
import org.junit.jupiter.api.Assertions;
76
import org.junit.jupiter.api.DisplayName;
87
import org.junit.jupiter.api.Nested;
@@ -15,6 +14,7 @@
1514
import com.spun.util.LambdaThreadLauncher;
1615
import com.spun.util.ObjectUtils;
1716
import com.spun.util.tests.TestUtils;
17+
import com.spun.util.tests.TestUtils.SourceDirectoryRestorer;
1818

1919
public class JUnit5StackTraceNamerTest
2020
{
@@ -69,9 +69,9 @@ void approvalFromInsideLambda() throws Exception
6969
{ throw ObjectUtils.throwAsError(caught[0]); }
7070
}
7171
@Test
72-
void testOverridingDirectoryFinder() throws Exception
72+
void testOverridingDirectoryFinder()
7373
{
74-
try (AutoCloseable autoCloseable = TestUtils
74+
try (SourceDirectoryRestorer sdr = TestUtils
7575
.registerSourceDirectoryFinder((c, s) -> new File("does not exist")))
7676
{
7777
StackTraceNamer name = new StackTraceNamer();

0 commit comments

Comments
 (0)