UTBotJava icon indicating copy to clipboard operation
UTBotJava copied to clipboard

Using futures in user code causes Fuzzer to generate tests which fail during execution

Open tyuldashev opened this issue 3 years ago • 0 comments

Description When code contains usage of CompletableFuture Fuzzer may generate tests which throw unexpected exception during execution.

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open UTBot Java project
  3. Set Test method generation: Fuzzer = 100%
  4. Generate tests for examples.threads.FutureExamples#changingCollectionInFutureWithoutGet
  5. 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.

tyuldashev avatar Oct 03 '23 14:10 tyuldashev