UTBotJava icon indicating copy to clipboard operation
UTBotJava copied to clipboard

UTBot claims that method is flaky while it looks determined

Open tyuldashev opened this issue 3 years ago • 0 comments

Description User gets warning about flaky method and mocked test when user code gets values from method outside the package.

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open UTBot project
  3. Generate tests for org.utbot.examples.mock.MockStaticMethodExample#useStaticMethod with option 'Mock everything outside the package' enabled and "Mock static methods" disabled.

Expected behavior Mock used to emulate Random.nextRandomInt() call. No warnings, because that method definition is:

    public static int nextRandomInt() {
        return 42;
    }

Actual behavior Generated test contains warning about flaky methods, see code below.

   @Test
    @DisplayName("useStaticMethod: value > 50 : False -> return 0")
    public void testUseStaticMethod_ValueLessOrEqual50() {
        MockedStatic mockedStatic = null;
        try {
            mockedStatic = mockStatic(Random.class);
            (mockedStatic.when(Random::nextRandomInt)).thenReturn(50);
            /* WARNING!!! Automatically used "Mockito static mocking" framework for mocking statics
            because execution encountered flaky methods
            To change this behaviour edit [Settings -> UtBot -> Force static mocking] */
            MockStaticMethodExample mockStaticMethodExample = new MockStaticMethodExample();

            int actual = mockStaticMethodExample.useStaticMethod();

            assertEquals(0, actual);
        } finally {
            mockedStatic.close();
        }
    }

Additional context Warning disappears when use enables option "Mock static methods". Even when nextRandomInt() is not static behavior is the same - there is warning about flaky method and usage "Mockito static mocking" framework. Issue is reproducible in previous version of plugin (2023.3).

tyuldashev avatar Sep 13 '23 08:09 tyuldashev