Подтвердить что ты не робот

set mock return value для любого целочисленного входного параметра

when(candidateService.findById(1)).thenReturn(new Candidate());

Я хочу расширить это поведение для любого целого (не обязательно для 1)

Если я разыграю

when(candidateService.findById( any(Integer.class)  )).thenReturn(new Candidate());

У меня ошибка компиляции

Метод findById (Integer) в типе CandidateService не применим для аргументов (Matcher)

ОБНОВИТЬ

импорт:

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.HashSet;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
4b9b3361

Ответ 1

Попробуйте anyInt():

when(candidateService.findById( anyInt())).thenReturn(new Candidate());

Например, у меня есть anyLong() в моем проекте:

when(dao.getAddress(anyLong())).thenReturn(Arrays.asList(dto));

EDIT: вы должны импортировать:

import static org.mockito.Matchers.anyInt;