我需要模拟以下枚举:
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
我怎么解决这个问题?
解决方法:
您的testFindByStatus试图断言findByStatus不返回null.
如果方法以相同的方式工作,无论personStatus param的值如何,只需传递其中一个:
@Test
public void testFindByStatus() throws ParseException {
List<Person> personlist = PersonRepository.findByStatus(WORKING);
assertThat(personlist, notNullValue());
}
如果其他可能值的行为可能不同,您可以测试每个值:
@Test
public void testFindByStatus() throws ParseException {
for (PersonStatus status : PersonStatus.values()) {
List<Person> personlist = PersonRepository.findByStatus(status);
assertThat(personlist, notNullValue());
}
}