Skip to content

Incorrect cast with compilation error in generated test for Spring method  #2074

Closed
@EgorkaKulikov

Description

@EgorkaKulikov

To Reproduce

Consider the project spring-boot-2-rest-service-validation from https://github.com/in28minutes/spring-boot-examples
Generate tests for method StudentResource.createStudent

Expected behavior

Generated test can be compiled.

Actual behavior

There is a compilation error in the following line

Student actual = ((Student) studentResource.createStudent(null));

As far as this method returns ResponceEntity<Object> our selection of type Student is a little bit strange

Additional context

Possibly the problem is in the previous lines of this test:

@Test
    @DisplayName("createStudent: ")
    public void testCreateStudent() {
        org.mockito.MockedStatic mockedStatic = null;
        org.mockito.MockedStatic mockedStatic1 = null;
        try {
            mockedStatic = mockStatic(ServletUriComponentsBuilder.class);
            ServletUriComponentsBuilder servletUriComponentsBuilderMock = mock(ServletUriComponentsBuilder.class);
            UriComponentsBuilder uriComponentsBuilderMock = mock(UriComponentsBuilder.class);
            UriComponents uriComponentsMock = mock(UriComponents.class);
            (when(uriComponentsMock.toUri())).thenReturn(((URI) null));
            (when(uriComponentsBuilderMock.buildAndExpand(any(Object[].class)))).thenReturn(uriComponentsMock);
            (when(servletUriComponentsBuilderMock.path(any(String.class)))).thenReturn(uriComponentsBuilderMock);
            (mockedStatic.when(ServletUriComponentsBuilder::fromCurrentRequest)).thenReturn(servletUriComponentsBuilderMock);
            mockedStatic1 = mockStatic(ResponseEntity.class);
            BodyBuilder bodyBuilderMock = mock(BodyBuilder.class);
            (when(bodyBuilderMock.build())).thenReturn(((ResponseEntity) null));
            (mockedStatic1.when(() -> ResponseEntity.created(any()))).thenReturn(bodyBuilderMock);
            (when(studentRepositoryMock.save(any()))).thenReturn(studentMock);

            Student actual = ((Student) studentResource.createStudent(null));

            assertNull(actual);
        } finally {
            mockedStatic.close();
            mockedStatic1.close();
        }
    }

Note that displayNamre is not very good, requires further investigation.

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorcomp-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