Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mock for a parent class is created #2644

Open
Assignees
Labels
comp-codegenIssue is related to code generator comp-springIssue is related to Spring projects support ctg-bugIssue is a bug
@alisevych

Description

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

Metadata

Metadata

Labels

comp-codegenIssue is related to code generator comp-springIssue is related to Spring projects support 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

      AltStyle によって変換されたページ (->オリジナル) /