Mockito 사용 예시
@Test
@DisplayName("회원 가입")
public void addUser() {
// given
UserDto userDto = UserDto.builder()
.email(email)
.username(username)
.password(password)
.name(name)
.build();
when(userRepository.save(any())).thenReturn(user);
// when
UserDto saved_user = userService.signUp(userDto);
// then
assertThat(saved_user.getUsername()).isEqualTo(userDto.getUsername());
}
Argument matchers
any() | nulls, varargs를 포함해서 어떤 것이든 매칭 |
anyBoolean() | Any boolean or non-null Boolean |
anyByte() | Any byte or non-null Byte |
anyChar() | Any char or non-null Character |
anyInt() | Any int or non-null Integer |
anyLong() | Any long or non-null Long |
anyFloat() | Any float or non-null Float |
anyDouble() | Any double or non-null Double |
anyShort() | Any short or non-null Short |
anyString() | Any non-null String |
anyList() | Any non-null List |
anySet() | Any non-null Set |
anyMap() | Any non-null Map |
anyCollection | Any non-null Collection |
anyIterable | Any non-null Iterable |
// 예시
when(mockedList.get(anyInt())).thenReturn("element");
verify(객체).메소드 | 메소드 호출 검증 ex) List mockedList = mock(List.class); mockedList.add("one"); verify(mockedList).add("one"); |
참고👇
https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/ArgumentMatchers.html
https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#3
https://minkwon4.tistory.com/181
반응형
'Test > Mockito' 카테고리의 다른 글
[Error] org.mockito.exceptions.misusing.PotentialStubbingProblem: Strict stubbing argument mismatch. Please check: (0) | 2023.03.05 |
---|