如何在2.7.x中使用Mockito.doReturn
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在2.7.x中使用Mockito.doReturn相关的知识,希望对你有一定的参考价值。
Mockito版:v2.7.5/19
。
例外:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.mckesson.dex.dao.code.CodeDaoMockTest.testExcluded(CodeDaoMockTest.java:33)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
这是我的代码:
@RunWith(MockitoJUnitRunner.class)
public class CodeDaoMockTest
{
@Mock( name = "entityManager") private HibernateEntityManager entityManager;
@Spy @InjectMocks
private CodeDao dao;
@Test
public void testExcluded() throws Exception
{
LabTestClassification ltc1 = new LabTestClassification();
LabTestClassification ltc2 = new LabTestClassification();
Mockito.doReturn( 533965, 533966)
.when( dao.getNextCodeIntegerFromSequence( ltc1 ) );
值得注意的是,如果我这样写:
Mockito.when( dao.getNextCodeIntegerFromSequence( ltc1 ) ).thenReturn( 533965 );
我在调用entityManager
时得到一个空指针。我的理解是,如果我使用doReturn
然后实际的getNext...
永远不会被调用,这是目标。
答案
你很亲密尝试使用不同的语法:
Mockito.doReturn(533965).when(dao).getNextCodeIntegerFromSequence(ltc1);
以上是关于如何在2.7.x中使用Mockito.doReturn的主要内容,如果未能解决你的问题,请参考以下文章