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

Introduce fuzzing in Spring unit tests #2529

Merged
merged 14 commits into from
Aug 21, 2023
Merged

Conversation

IlyaMuravjov
Copy link
Collaborator

Description

Fixes #2321

To be compatible with Spring unit test paradigm fuzzer is made to mock all third party classes (other than class under test and some well know types, e.g. primitives, lists, and so on) and construct UtCompositeModel for class under test, so mocks can be injected into it with @InjectMock.

Example of fuzzer generated test (ownerRepositoryMock is injected into ownerController via @InjectMocks):

@Test
public void testProcessFindFormByFuzzer() {
	Page pageMock1 = mock(Page.class);
	(when(pageMock1.isEmpty())).thenReturn(false);
	(when(pageMock1.getTotalElements())).thenReturn(0L, 0L);
	List listMock1 = mock(List.class);
	(when(pageMock1.getContent())).thenReturn(listMock1);
	(when(pageMock1.getTotalPages())).thenReturn(0);
	(when(ownerRepositoryMock.findByLastName(any(), any()))).thenReturn(pageMock1);
	Owner ownerMock = mock(Owner.class);
	(when(ownerMock.getLastName())).thenReturn(((String) null), ((String) null));
	((doNothing()).when(ownerMock)).setLastName(any());
	BindingResult resultMock = mock(BindingResult.class);
	Model modelMock = mock(Model.class);
	Model modelMock1 = mock(Model.class);
	(when(modelMock.addAttribute(any(), any()))).thenReturn(modelMock1, modelMock1, modelMock1, modelMock1, modelMock1);

	String actual = ownerController.processFindForm(1073741824, ownerMock, resultMock, modelMock);

	String expected = "owners/ownersList";

	assertEquals(expected, actual);
}

How to test

Manual tests

Generate unit tests with fuzzing ratio set to 100% for OwnerController.

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@IlyaMuravjov IlyaMuravjov added ctg-enhancement New feature, improvement or change request comp-fuzzing Issue is related to the fuzzing comp-spring Issue is related to Spring projects support labels Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-fuzzing Issue is related to the fuzzing comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate how to use fuzzing in Spring unit tests
2 participants