Skip to content

Commit 52cb796

Browse files
committed
GH-354: Add missing test case for NPE as a result of misbehaving compiler
1 parent 9f31aa4 commit 52cb796

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/compilers/impl/JctCompilationFactoryImplTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static io.github.ascopes.jct.tests.helpers.Fixtures.someBinaryName;
2121
import static io.github.ascopes.jct.tests.helpers.Fixtures.someFlags;
2222
import static io.github.ascopes.jct.tests.helpers.Fixtures.someLinesOfText;
23+
import static io.github.ascopes.jct.tests.helpers.Fixtures.someText;
2324
import static io.github.ascopes.jct.tests.helpers.Fixtures.someTraceDiagnostic;
2425
import static io.github.ascopes.jct.utils.IterableUtils.flatten;
2526
import static java.util.stream.Collectors.toList;
@@ -655,6 +656,35 @@ void exceptionsDuringCompilationGetWrappedAndRethrown() throws IOException {
655656
.hasCause(cause);
656657
}
657658

659+
@DisplayName("Compilers returning null outcomes will be raised as an exception")
660+
@Test
661+
void compilersReturningNullOutcomesWillBeRaisedAsAnException() throws IOException {
662+
// Given
663+
var task = mock(CompilationTask.class);
664+
var name = someText();
665+
666+
when(jctCompiler.getName())
667+
.thenReturn(name);
668+
669+
when(javaCompiler.getTask(any(), any(), any(), any(), any(), any()))
670+
.thenReturn(task);
671+
when(task.call())
672+
.thenReturn(null);
673+
674+
// Do not inline this, it will break in Mockito's stubber backend.
675+
var fileObjects = Set.of(somePathFileObject(someBinaryName()));
676+
when(fileManager.list(any(), any(), any(), anyBoolean()))
677+
.thenReturn(fileObjects);
678+
679+
// Then
680+
assertThatThrownBy(() -> doCompile(null))
681+
.isInstanceOf(JctCompilerException.class)
682+
.hasMessage("Failed to perform compilation, an unexpected exception was raised")
683+
.cause()
684+
.hasMessage("Compiler %s task .call() method returned null unexpectedly!", name)
685+
.isInstanceOf(NullPointerException.class);
686+
}
687+
658688
@DisplayName("The compilation result holds the failOnWarnings flag from the compiler")
659689
@ValueSource(booleans = {true, false})
660690
@ParameterizedTest(name = "for compiler.isFailOnWarnings() = {0}")

0 commit comments

Comments
 (0)