【发布时间】:2016-05-05 11:17:27
【问题描述】:
我需要模拟以下枚举:
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吗?你不能只使用实际的实例吗? -
我认为您不想模拟枚举 - 您想在测试中传递枚举的各种值并检查结果是否符合预期。
-
@assylias 请提供答案,说明如何这样做
-
为什么要模拟枚举?
-
您只需要传递
PersonStatus.WORKING或PersonStatus.SICK之类的参数即可。不管它是什么,然后用你的预期结果进行检查。
标签: java unit-testing enums mockito final