Skip to content

Fully qualified names in static mocks #2175

@alisevych

Description

@alisevych

Description

Need to use short class names instead of fully qualified ones in static mocks.

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open spring-petclinic project
  3. Generate tests for PetValidator

Expected behavior

import org.springframework.util.StringUtils is expected to use short class name StringUtils in mockStatic.

Actual behavior

There are fully qualified name for org.springframework.util.StringUtils.class in mockStatic definition.

Screenshots, logs

	public void testValidate_PetGetBirthDateNotEqualsNull() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		org.mockito.MockedStatic mockedStatic = null;
		try {
			mockedStatic = mockStatic(org.springframework.util.StringUtils.class);
			(mockedStatic.when(() -> org.springframework.util.StringUtils.hasLength(any(String.class)))).thenReturn(false);
			PetValidator petValidator = new PetValidator();
			NamedEntity objMock = mock(NamedEntity.class);
			(when(objMock.getName())).thenReturn(((String) null));
			(when(objMock.isNew())).thenReturn(false);
			LocalDate localDateMock = mock(LocalDate.class);
			Class petClazz = Class.forName("org.springframework.samples.petclinic.owner.Pet");
			Method getBirthDateMethod = petClazz.getDeclaredMethod("getBirthDate");
			getBirthDateMethod.setAccessible(true);
			Object[] getBirthDateMethodArguments = new Object[0];
			(when(getBirthDateMethod.invoke(objMock, getBirthDateMethodArguments))).thenReturn(localDateMock);
			Errors errorsMock = mock(Errors.class);
			((doNothing()).when(errorsMock)).rejectValue(any(), any(), any());

			petValidator.validate(objMock, errorsMock);
		} finally {
			mockedStatic.close();
		}
	}

Environment

IntelliJ IDEA version - 2023.1 Ultimate
JDK - 17

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions