KEMBAR78
Ito3 Mock: Cheat Sheet | PDF | Method (Computer Programming) | Computer Programming
0% found this document useful (0 votes)
36 views1 page

Ito3 Mock: Cheat Sheet

This document is a cheat sheet for using Mockito to create and define mocks, spies, and verify behavior in tests. It shows how to create mocks and spies, define return values and exceptions, capture arguments, and verify interactions and behavior.

Uploaded by

afrisilman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

Ito3 Mock: Cheat Sheet

This document is a cheat sheet for using Mockito to create and define mocks, spies, and verify behavior in tests. It shows how to create mocks and spies, define return values and exceptions, capture arguments, and verify interactions and behavior.

Uploaded by

afrisilman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

mockito3 cheat sheet

by Adrian Więch

CREATE

AR GUMENT CA PTORS

//mocks: default values returned by default

1. Define:

ClassName mock = mock(ClassName.class) ;

@Captor

or use: @Mock

private ArgumentCaptor<Double> 


doubleCaptor;

//spies: class logic used by default

ClassName spy = spy(ClassName.class) ;

2. Capture :

or use: @Spy

verify(mock, times(1)).method(eq(val), 


doubleCaptor.capture());

//static mocks: use try-with-resources

MockedStatic<ClassName> mock =

3. Get va lue:

mockStatic( ClassName.class));

double capturedArgument = 


doubleCaptor.getValue();

DEFINE BEHAVIOUR

//mocks

MOCKITO BD D

- when(mock.sampleMethod()) 


.thenReturn(sampleVal);
// when...thenReturn

- when(mock.sampleMethod())
 given(mock.method()) .willReturn(val);

.thenThrow(SampleException.class);

- when(mock.sampleMethod())
// veri fy(class, times( 1)).method()

.thenAnswer(inv -> ...)

then(mock).should(times(1)).method();

//spies or void methods

- doReturn(value)
 SAMPLE TE ST WITH A MOCK


.when(spy).method();

- doThrow(new ExceptionClass())


.when(spy).metohd();
@ExtendWith(MockitoExtension.class)

- doNothing().when(spy).method();

class ForCheatSheet {

@InjectMocks

//static mocks

private BookingService bookingService;

staticMock.when(() ->

StaticClass.method().thenReturn(val);

@Mock

private RoomService roomServiceMock;

//argument matchers

@Test

when(mock.sampleMethod(any(), 

void sample() {

anyDouble())).thenReturn(sampleVal);

// given

when(this.roomServiceMock.getAvailableRooms())

.thenReturn(Collections.singletonList(new 


Room("Room 1", 5)));

VERIFY BEHAVIOUR

int expected = 5;

// when

//method() invoked once

int actual = 

- verify(mockClass, times(1)).method();

bookingService.getAvailablePlaceCount();

//method never invoked


// then

- verify(mockClass, never()).method();

assertEquals(expected, actual);

//no more methods invoked

- verifyNoMoreInteractions(mockClass);

You might also like