Skip to content

byte array is returned in mock for JpaRepository method while entity is expected #2630

@alisevych

Description

@alisevych

Description

byte array is returned in mock for JpaRepository method while entity is expected
Test is not compiling

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open Medical-Web-App project, develop-new branch
  3. Generate Unit tests for NotificationService with @SpringBootApplication configuration

Expected behavior

Compiling tests should be generated

Actual behavior

byte array is set to be returned from JpaRepository<T, ID> getById method mock,
while UserRepository extends it with T = User
3 similar tests are not compiling.

Screenshots, logs

@Repository
public interface UserRepository extends JpaRepository<User, Long>
public interface JpaRepository<T, ID>
    T getById(ID id);
    @Test
    @DisplayName("getAllNotifications: user = userRepository.getById(user_id) : True -> ThrowClassCastException")
    public void testGetAllNotifications_ThrowClassCastException() {
        byte[] byteArray = {};
        (when(userRepositoryMock.getById(any()))).thenReturn(byteArray);
        
        /* This test fails because method [com.app.medicalwebapp.services.NotificationService.getAllNotifications] produces [java.lang.ClassCastException: class [B cannot be cast to class com.app.medicalwebapp.model.User ([B is in module java.base of loader 'bootstrap'; com.app.medicalwebapp.model.User is in unnamed module of loader org.utbot.instrumentation.process.HandlerClassesLoader @10eb7238)]
            com.app.medicalwebapp.repositories.UserRepository$MockitoMock$YIpF9DQ3.getById(Unknown Source)
            com.app.medicalwebapp.repositories.UserRepository$MockitoMock$YIpF9DQ3.getById(Unknown Source)
            com.app.medicalwebapp.services.NotificationService.getAllNotifications(NotificationService.java:54) */
        notificationService.getAllNotifications(null);
    }

Environment

IntelliJ IDEA version - Ultimate 2023.2
Project - Maven
JDK - 11

Metadata

Metadata

Assignees

Labels

comp-springIssue is related to Spring projects supportcomp-symbolic-engineIssue is related to the symbolic execution enginectg-bugIssue is a bugstatus-duplicateThis issue or pull request already exists

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions