Skip to content

Generic type MessageResponse of Http response is not applied is assertion part #2598

Closed
@alisevych

Description

@alisevych

Description

Unit tests generation with Fuzzing for a Controller with Spring application configuration applied.

MessageResponse of Http response is not applied when asserting body messages of ResponseEntity.
While this type has been found and applied when creating expected ResponseEntity.
This type is generic type of HttpResponse. In ResponseEntity#registerUser it is applied in return statement.

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA 2023.2
  2. Open Medical-Web-App project
  3. Generate Unit tests for AuthController#registerUser using MedicalWebApp configuration
  4. Find FUZZER: SUCCESSFUL EXECUTIONS for method registerUser(com.app.medicalwebapp.controllers.requestbody.SignUpRequest)

Expected behavior

actual.getBody() and expectedBody.getMessage() result should be cast to MessageResponse

Actual behavior

body of ResponseEntity is typed as Object
That's why getMessage() cannot be called on it.
Tests are not compiling.

Screenshots, logs

    ///region FUZZER: SUCCESSFUL EXECUTIONS for method registerUser(com.app.medicalwebapp.controllers.requestbody.SignUpRequest)

    /**
     * @utbot.classUnderTest {@link AuthController}
     * @utbot.methodUnderTest {@link AuthController#registerUser(SignUpRequest)}
     */
    @Test
    @DisplayName("registerUser: signUpRequest = mock()")
    public void testRegisterUser() throws Exception {
        (when(userRepositoryMock.existsByUsername(any()))).thenReturn(false);
        User userMock = mock(User.class);
        (when(userRepositoryMock.save(any()))).thenReturn(userMock);
        (when(passwordEncoderMock.encode(any()))).thenReturn("abc");
        SignUpRequest signUpRequestMock = mock(SignUpRequest.class);
        (when(signUpRequestMock.getUsername())).thenReturn(((String) null));
        (when(signUpRequestMock.getInitials())).thenReturn("");
        (when(signUpRequestMock.getFirstname())).thenReturn("\u042D\u0442\u043E\u0442 \u043B\u043E\u0433\u0438\u043D \u0443\u0436\u0435 \u0437\u0430\u043D\u044F\u0442");
        (when(signUpRequestMock.getLastname())).thenReturn("abc");
        (when(signUpRequestMock.getPatronymic())).thenReturn("\n\t\r");
        (when(signUpRequestMock.getChosenRole())).thenReturn("#$\\\"'");
        (when(signUpRequestMock.getPassword())).thenReturn("10");
        (when(signUpRequestMock.getSpecialization())).thenReturn(((String) null));
        (when(signUpRequestMock.getSpecializedDiagnoses())).thenReturn("");
        (when(signUpRequestMock.getExperience())).thenReturn(0);
        (when(signUpRequestMock.getWorkplace())).thenReturn("\u042D\u0442\u043E\u0442 \u043B\u043E\u0433\u0438\u043D \u0443\u0436\u0435 \u0437\u0430\u043D\u044F\u0442");
        (when(signUpRequestMock.getEducation())).thenReturn("XZ");
        (when(signUpRequestMock.getPrice())).thenReturn(0);

        ResponseEntity actual = authController.registerUser(signUpRequestMock);

        ResponseEntity expected = ((ResponseEntity) createInstance("org.springframework.http.ResponseEntity"));
        HttpStatus status = HttpStatus.OK;
        setField(expected, "org.springframework.http.ResponseEntity", "status", status);
        Object headers = createInstance("org.springframework.http.ReadOnlyHttpHeaders");
        MultiValueMapAdapter headers1 = ((MultiValueMapAdapter) createInstance("org.springframework.util.MultiValueMapAdapter"));
        Map targetMap = new LinkedHashMap();
        setField(headers1, "org.springframework.util.MultiValueMapAdapter", "targetMap", targetMap);
        setField(headers, "org.springframework.http.HttpHeaders", "headers", headers1);
        setField(expected, "org.springframework.http.HttpEntity", "headers", headers);
        MessageResponse body = ((MessageResponse) createInstance("com.app.medicalwebapp.controllers.requestbody.MessageResponse"));
        String message = "\u041F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044C \u0431\u044B\u043B \u0443\u0441\u043F\u0435\u0448\u043D\u043E \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043E\u0432\u0430\u043D";
        body.setMessage(message);
        setField(expected, "org.springframework.http.HttpEntity", "body", body);

        Object expectedStatus = getFieldValue(expected, "org.springframework.http.ResponseEntity", "status");
        Object actualStatus = getFieldValue(actual, "org.springframework.http.ResponseEntity", "status");
        assertEquals(expectedStatus, actualStatus);

        HttpHeaders expectedHeaders = expected.getHeaders();
        HttpHeaders actualHeaders = actual.getHeaders();
        // org.springframework.http.HttpHeaders is iterable or Map, use outer deep equals to iterate over
        assertTrue(deepEquals(expectedHeaders, actualHeaders));

        Object expectedBody = expected.getBody();
        Object actualBody = actual.getBody();
        String expectedBodyMessage = expectedBody.getMessage();
        String actualBodyMessage = actualBody.getMessage();
        assertEquals(expectedBodyMessage, actualBodyMessage);

    }

Environment

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

Additional context

When the following correction is made, the test passes:

            MessageResponse expectedBody = (MessageResponse) expected.getBody();
            MessageResponse actualBody = (MessageResponse) actual.getBody();

Metadata

Metadata

Labels

comp-codegenIssue is related to code generatorcomp-fuzzingIssue is related to the fuzzingcomp-springIssue is related to Spring projects supportctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions