UTBotJava
UTBotJava copied to clipboard
Using futures in user code causes Fuzzer to generate tests which fail during execution
Description
When code contains usage of CompletableFuture Fuzzer may generate tests which throw unexpected exception during execution.
To Reproduce
- Install UnitTestBot plugin built from main in IntelliJ IDEA
- Open UTBot Java project
- Set Test method generation: Fuzzer = 100%
- Generate tests for
examples.threads.FutureExamples#changingCollectionInFutureWithoutGet - Run generated tests
Expected behavior Tests without exception pass
Actual behavior Half of generated tests fail.
Screenshots, logs Code under test
public int changingCollectionInFutureWithoutGet() {
List<Integer> values = new ArrayList<>();
final CompletableFuture<Void> future = CompletableFuture.runAsync(() -> values.add(42));
return values.get(0);
}
One of generated tests:
@Test
@DisplayName("changingCollectionInFutureWithoutGet: -> return 42")
public void testChangingCollectionInFutureWithoutGetReturns42() {
FutureExamples futureExamples = new FutureExamples();
int actual = futureExamples.changingCollectionInFutureWithoutGet();
assertEquals(42, actual);
}
Exception during execution:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at examples.threads.FutureExamples.changingCollectionInFutureWithoutGet(FutureExamples.java:41)
at examples.threads.FutureExamplesTest.testChangingCollectionInFutureWithoutGetReturns42(FutureExamplesTest.java:23)
Additional context Symbolic engine doesn't generate tests which should pass.