image.png

Taxonomy of test doubles

image.png

Motivation for mock objects

image.png

EasyMock

@ExtendWith(EasyMockExtension.class)
class EnrollmentServiceTest {

	@TestSubject // Instantiate the SUT
	private EnrollmentService enrollmentService = new EnrollmentService();
	
	@Mock // Create the mock object
	private Course courseMock;
	
	@Test
	void testEnrollStudentSuccessful() {

		Student student = new Student();
		int expectedSize = student.getCourses().size() + 1; // Specify the expected behavior
		expect(courseMock.enroll(student)).andReturn(true);
		
		replay(courseMock); // Make the mock object ready to play
		
		enrollmentService.enroll(student, courseMock); // Execute the SUT
		
		assertEquals(expectedSize, student.getCourses().size()); // Validate observed against expected behavior
		verify(courseMock); //Verify that enroll() was invoked on courseMock once
		}
}

Best practices for testing