Skip to content

TBD Logger is mocked #2189

Open
Open
@alisevych

Description

@alisevych

Description

Logger is in the list of classes to be force mocked.
Why?
Usually user does not want to test logger. So mocking of logger allows to test user code - without danger to face errors with logging.
However this can be turned off with option in "forceStaticMocking" in UtBotJavaApi. And on UI in IDEA:

image

Expected behavior

(TBD) Probably we need to add comment to the Logger mock like:

// Logger is mocked to exclude it from testing. 
// You can turn this option off here: https://github.com/UnitTestBot/UTBotJava/wiki/Fine-tune-test-generation#classes-to-be-forcedly-mocked
// Then Logger will be analyzed too.

Example

  1. Generate tests with default settings for org.utbot.examples.mixed.LoggerExample
  2. The following tests are generated:
    public void testExample_LoggerInfo() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
        MockedStatic mockedStatic = null;
        try {
            Class loggerExampleClazz = Class.forName("org.utbot.examples.mixed.LoggerExample");
            Logger prevLogger = ((Logger) getStaticFieldValue(loggerExampleClazz, "logger"));
            try {
                Logger loggerMock = mock(Logger.class);
                ((doNothing()).when(loggerMock)).debug(any());
                ((doNothing()).when(loggerMock)).error(any());
                ((doNothing()).when(loggerMock)).info(any());
                setStaticField(loggerExampleClazz, "logger", loggerMock);
                mockedStatic = mockStatic(LoggerFactory.class);
                (mockedStatic.when(() -> LoggerFactory.getLogger(any(Class.class)))).thenReturn(loggerMock);
                LoggerExample loggerExample = new LoggerExample();

                int actual = loggerExample.example();

                assertEquals(15, actual);
            } finally {
                setStaticField(LoggerExample.class, "logger", prevLogger);
            }
        } finally {
            mockedStatic.close();
        }
    }

Environment

IntelliJ IDEA

Potential alternatives

Logger is mocked on the step of Symbolic execution.
If mocks are not included in the resulting test - we cannot rely on symbolic execution results.
Generated test cases should be equal to found Symbolic executions.

Possible issues:

  • There can be values or method calls inside logger record. And if they are null or throw an exception - the execution will fail.
  • Debug/info logger level can be turned off. And is there are logger.debug(...) - there will be exception too.

Context

                Logger loggerMock = mock(Logger.class);
                ((doNothing()).when(loggerMock)).info(any(String.class), any(Object.class));

We have good explanation in User guide

Fuzzer is generating tests without mocks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-codegenIssue is related to code generatorctg-enhancementNew feature, improvement or change requestctg-questionFurther information is requestedspec-uxIssue is influencing user experience

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions