Skip to content

Mockito dependency needed by tests, but not added by plugin #2740

Description

Description

UnitTestBot generates a test suite that uses several Mockito API components. However, no suitable Mockito dependency is present or is added by the plugin.

To Reproduce

Steps to reproduce the behavior:

  1. Check out https://github.com/atn-lyn/lyn-parents. For the issue reported here, the current master head revision of this repository was atn-lyn/lyn-parents@3ff5ce7, but it is not clear whether this issue is sensitive to this specific revision.

  2. Add the following to pom.xml to explicitly select Java 1.8:

     <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
  3. Open this project in IntelliJ IDEA and link its pom.xml, thereby making this a Maven project.

  4. In the IntelliJ IDEA Project Settings, select a Java 1.8 SDK.

  5. If this import line in lyn-web/src/main/java/com/lyn/web/controller/GoodsController.java fails to resolve in the IDE, then delete it.

  6. Use the UnitTestBot IntelliJ IDEA plugin to generate tests for the com.lyn.web.controller.GoodsController.search(String, Pageable) method. Leave all UnitTestBot settings at their defaults, other than selecting this one method to generate tests for.

Expected behavior

UnitTestBot should have generated one or more tests that are ready to compile and run in the IDE.

Actual behavior

UnitTestBot generated a test class with multiple test methods:

Generated `GoodsControllerTest.java`
packagecom.lyn.web.controller;
importcom.lyn.common.cache.CacheService;
importcom.lyn.goods.api.service.GoodsService;
importcom.lyn.web.elastic.GoodsEsRepository;
importorg.elasticsearch.index.query.BoolQueryBuilder;
importorg.elasticsearch.index.query.QueryBuilders;
importorg.elasticsearch.index.query.TermQueryBuilder;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Ignore;
importorg.junit.Test;
importorg.mockito.InjectMocks;
importorg.mockito.Mock;
importorg.mockito.MockedConstruction;
importorg.mockito.MockedConstruction.Context;
importorg.mockito.MockedStatic;
importorg.springframework.data.domain.Pageable;
importorg.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
importorg.springframework.kafka.core.KafkaTemplate;
importstaticorg.mockito.ArgumentMatchers.any;
importstaticorg.mockito.ArgumentMatchers.anyInt;
importstaticorg.mockito.Mockito.*;
importstaticorg.mockito.MockitoAnnotations.openMocks;
publicfinalclassGoodsControllerTest {
@InjectMocksprivateGoodsControllergoodsController;
@MockprivateKafkaTemplatekafkaTemplateMock;
@MockprivateCacheServicecacheServiceMock;
@MockprivateGoodsEsRepositorygoodsEsRepositoryMock;
@MockprivateGoodsServicegoodsServiceMock;
privateAutoCloseablemockitoCloseable;
///region Test suites for executable com.lyn.web.controller.GoodsController.search///region SYMBOLIC EXECUTION: ERROR SUITE for method search(java.lang.String, org.springframework.data.domain.Pageable)/** * @utbot.classUnderTest {@link GoodsController} * @utbot.methodUnderTest {@link GoodsController#search(String, Pageable)} * @utbot.invokes {@link QueryBuilders#boolQuery()} * @utbot.invokes {@link QueryBuilders#termQuery(String, int)} * @utbot.invokes {@link BoolQueryBuilder#filter(org.elasticsearch.index.query.QueryBuilder)} * @utbot.throwsException {@link NullPointerException} in: boolBuilder.filter(QueryBuilders.termQuery("status", 1)); */@TestpublicvoidtestSearch_ThrowNullPointerException() throwsException {
MockedConstructionmockedConstruction = null;
MockedStaticmockedStatic = null;
try {
mockedConstruction = mockConstruction(NativeSearchQueryBuilder.class, (NativeSearchQueryBuildernativeSearchQueryBuilderMock, Contextcontext) -> {
});
mockedStatic = mockStatic(QueryBuilders.class);
(mockedStatic.when(QueryBuilders::boolQuery)).thenReturn(((BoolQueryBuilder) null));
(mockedStatic.when(() -> QueryBuilders.termQuery(any(String.class), anyInt()))).thenReturn(((TermQueryBuilder) null));
/* This test fails because method [com.lyn.web.controller.GoodsController.search] produces [java.lang.NullPointerException] */goodsController.search(null, null);
} finally {
mockedConstruction.close();
mockedStatic.close();
}
}
///endregion///region FUZZER: SECURITY for method search(java.lang.String, org.springframework.data.domain.Pageable)/** * @utbot.classUnderTest {@link GoodsController} * @utbot.methodUnderTest {@link GoodsController#search(String, Pageable)} */@Test(expected = ExceptionInInitializerError.class)
@Ignore(value = "Disabled due to sandbox")
publicvoidtestSearchWithNonEmptyString() throwsException {
PageablepagMock = mock(Pageable.class);
goodsController.search("#\"$\\'", pagMock);
}
///endregion///region FUZZER: ERROR SUITE for method search(java.lang.String, org.springframework.data.domain.Pageable)/** * @utbot.classUnderTest {@link GoodsController} * @utbot.methodUnderTest {@link GoodsController#search(String, Pageable)} */@Test(expected = NoClassDefFoundError.class)
publicvoidtestSearchThrowsNCDFEWithNonEmptyString() throwsException {
PageablepagMock = mock(Pageable.class);
goodsController.search("#$\\\"'", pagMock);
}
///endregion///endregion@BeforepublicvoidsetUp() {
mockitoCloseable = openMocks(this);
}
@AfterpublicvoidtearDown() throwsException {
mockitoCloseable.close();
}
}

However, this test class fails to compile and run in IntelliJ IDEA. Attempting to do so produces the following compilation errors:

/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:15:19
java: cannot find symbol
symbol: class MockedConstruction
location: package org.mockito
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:16:38
java: package org.mockito.MockedConstruction does not exist
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:17:19
java: cannot find symbol
symbol: class MockedStatic
location: package org.mockito
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:25
java: cannot find symbol
symbol: static openMocks
location: class
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:59:9
java: cannot find symbol
symbol: class MockedConstruction
location: class com.lyn.web.controller.GoodsControllerTest
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:60:9
java: cannot find symbol
symbol: class MockedStatic
location: class com.lyn.web.controller.GoodsControllerTest
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:62:34
java: cannot find symbol
symbol: method mockConstruction(java.lang.Class<org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder>,(NativeSea[...]->{ })
location: class com.lyn.web.controller.GoodsControllerTest
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:62:139
java: cannot find symbol
symbol: class Context
location: class com.lyn.web.controller.GoodsControllerTest
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:64:28
java: cannot find symbol
symbol: method mockStatic(java.lang.Class<org.elasticsearch.index.query.QueryBuilders>)
location: class com.lyn.web.controller.GoodsControllerTest
/path/to/project/lyn-parents/lyn-web/utbot_tests/com/lyn/web/controller/GoodsControllerTest.java:111:28
java: cannot find symbol
symbol: method openMocks(com.lyn.web.controller.GoodsControllerTest)
location: class com.lyn.web.controller.GoodsControllerTest

The UnitTestBot IDE plugin already modifies pom.xml files to add JUnit dependencies as needed. It should add a suitable Mockito dependency as well.

Environment

  • IntelliJ IDEA 2023.2.6 (Ultimate Edition)
  • JetBrains UnitTestBot plugin version 2023.10
  • All UnitTestBot plugin settings were left at their default values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ctg-bugIssue is a bug

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions