Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock for a parent class is created #2644

Open
alisevych opened this issue Oct 4, 2023 · 0 comments
Open

Mock for a parent class is created #2644

alisevych opened this issue Oct 4, 2023 · 0 comments
Assignees
Labels
comp-codegen Issue is related to code generator comp-spring Issue is related to Spring projects support ctg-bug Issue is a bug

Comments

@alisevych
Copy link
Member

alisevych commented Oct 4, 2023

Description

Mock of a parent class is created, which is then tried to be cast to the needed class.

To Reproduce

  1. Install -IU- UnitTestBot plugin in IntelliJ IDEA
  2. Open spring-petclinic
  3. Generate ad Run Unit tests for PetValidator with PetClinicApplication

Expected behavior

Mock of Pet.class is expected to be used.

Actual behavior

Mock of NamedEntity.class is created, and then cast to Pet is made.
5 of 11 tests fail on ClassCastException thrown from test code.

Screenshots, logs

	@Test
	@DisplayName("validate: errors.rejectValue(\"type\", REQUIRED, REQUIRED) : True -> ThrowNullPointerException")
	public void testValidate_ThrowNullPointerException_2() {
		MockedStatic mockedStatic = null;
		try {
			mockedStatic = mockStatic(StringUtils.class);
			(mockedStatic.when(() -> StringUtils.hasLength(any(String.class)))).thenReturn(true);
			PetValidator petValidator = new PetValidator();
			NamedEntity objMock = mock(NamedEntity.class);
			(when(objMock.getName())).thenReturn(((String) null));
			(when(objMock.isNew())).thenReturn(true);
			(when((((Pet) objMock)).getType())).thenReturn(((PetType) null));

			/* This test fails because method [org.springframework.samples.petclinic.owner.PetValidator.validate] produces [java.lang.NullPointerException] */
			petValidator.validate(objMock, null);
		} finally {
			mockedStatic.close();
		}
	}

image

Environment

IntelliJ IDEA version - Ultimate 2023.2
Project - Gradle
JDK - 17

@alisevych alisevych added ctg-bug Issue is a bug comp-codegen Issue is related to code generator labels Oct 4, 2023
@alisevych alisevych added the comp-spring Issue is related to Spring projects support label Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator comp-spring Issue is related to Spring projects support ctg-bug Issue is a bug
Projects
Status: Todo
Development

No branches or pull requests

2 participants