Skip to content

Unnecessary mock recreations in Spring test method #2196

Closed
@sofurihafe

Description

@sofurihafe

Description

There are a lot of recreations of Injected mocks and mocks in Spring test methods.

To Reproduce

  1. Generate tests for OwnerController in spring-petclinic.
  2. Among generated tests there'll be ones with unnecessary recreations, e.g.
@InjectMocks
public OwnerController ownerController;

@Mock
public OwnerRepository ownerRepositoryMock;

@Test
@DisplayName("showOwner: IntegerValueOf -> return mav")
public void testShowOwner_ModelAndViewAddObject() throws ... {
MockedConstruction mockedConstruction = null;
        try {
                mockedConstruction = mockConstruction(...);
              ->OwnerRepository ownerRepositoryMock1 = mock(OwnerRepository.class);
                (when(ownerRepositoryMock1.findById(any()))).thenReturn(((Owner) null));
	      ->OwnerController ownerController1 = new OwnerController(ownerRepositoryMock1);
        
	        ...
        
        } finally {
	        ...
        }
}

Expected behavior

Already created variables must be used.

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