Java 使用 Mockito 模拟枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37049218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mocking an enum using Mockito?
提问by java123999
I need to mock the following enum:
我需要模拟以下枚举:
public enum PersonStatus
{
WORKING,
HOLIDAY,
SICK
}
This is because it is used in the following class that I am testing:
这是因为它在我正在测试的以下类中使用:
Class under test:
被测类:
public interface PersonRepository extends CrudRepository<Person, Integer>
{
List<Person> findByStatus(PersonStatus personStatus);
}
Here is my current test attempt:
这是我目前的测试尝试:
Current test:
当前测试:
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());
}
}
Which Gives following error:
这给出了以下错误:
error:
错误:
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class PersonStatus
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
How can I solve this?
我该如何解决这个问题?
采纳答案by assylias
Your testFindByStatus
is trying to assert that the findByStatus
does not return null.
您testFindByStatus
试图断言findByStatus
不返回空值。
If the method works the same way regardless of the value of the personStatus
param, just pass one of them:
如果无论personStatus
参数的值如何,该方法都以相同的方式工作,则只需传递其中之一:
@Test
public void testFindByStatus() throws ParseException {
List<Person> personlist = PersonRepository.findByStatus(WORKING);
assertThat(personlist, notNullValue());
}
If the behaviour may be different for the other possible values, you can test each of them:
如果其他可能值的行为可能不同,您可以测试每个值:
@Test
public void testFindByStatus() throws ParseException {
for (PersonStatus status : PersonStatus.values()) {
List<Person> personlist = PersonRepository.findByStatus(status);
assertThat(personlist, notNullValue());
}
}
回答by GhostCat
Just to complete the picture:
只是为了完成图片:
The latest version of Mockito 2 very well supports mocking of final classes. But you have to explicitly enable this new experimental feature first!
最新版本的 Mockito 2 很好地支持模拟最终类。但是您必须首先明确启用这个新的实验性功能!
( see hereon how to do that - it boils down to add a file mockito-extensions/org.mockito.plugins.MockMaker
to your classpath, containing the value mock-maker-inline
)
(请参阅此处了解如何执行此操作 - 归结为将文件添加mockito-extensions/org.mockito.plugins.MockMaker
到类路径中,其中包含值mock-maker-inline
)
But of course: you only mock something if you have to. Your desire to mock Enum instances is most likely driven by either not understanding that - or because you created hard to test code here. In that sense the real answer would be to look into ways that avoid this kind of mocking in the first place.
但当然:只有在必要时才嘲笑某些东西。您想要模拟 Enum 实例的原因很可能是因为不理解这一点 - 或者因为您在这里创建了难以测试的代码。从这个意义上说,真正的答案是首先寻找避免这种嘲笑的方法。