我需要模拟以下枚举:使用Mockito嘲笑枚举?
枚举
public enum PersonStatus
{
WORKING,
HOLIDAY,
SICK
}
这是因为它是在我测试下面的类中:
类测试:
public interface PersonRepository extends CrudRepository<Person, Integer>
{
List<Person> findByStatus(PersonStatus personStatus);
}
这是我目前的测试诱惑:
电流测试:
public class PersonRepositoryTest {
private final Logger LOGGER = LoggerFactory.getLogger(PersonRepositoryTest.class);
//Mock the PersonRepository class
@Mock
private PersonRepository PersonRepository;
@Mock
private PersonStatus personStatus;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
assertThat(PersonRepository, notNullValue());
assertThat(PersonStatus, notNullValue());
}
@Test
public void testFindByStatus() throws ParseException {
List<Person> personlist = PersonRepository.findByStatus(personStatus);
assertThat(personlist, notNullValue());
}
}
这给以下错误:
错误:
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class PersonStatus
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
我怎样才能解决这个问题?
你确定*你需要模拟'PersonStatus'吗?你不能只使用实际的实例吗? – khelwood
我不认为你想嘲笑枚举 - 你想在你的测试中传递枚举的各种值,并检查结果是否如预期。 – assylias
@assylias请提供答案,显示如何做 – java123999