Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在2.7.x中使用Mockito.doReturn_Java_Unit Testing_Mocking_Mockito - Fatal编程技术网

Java 如何在2.7.x中使用Mockito.doReturn

Java 如何在2.7.x中使用Mockito.doReturn,java,unit-testing,mocking,mockito,Java,Unit Testing,Mocking,Mockito,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. Exa

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);

如果你需要进一步阅读,这里有一篇我写的文章。

你很接近了。尝试使用稍微不同的语法:

Mockito.doReturn(533965).when(dao).getNextCodeIntegerFromSequence(ltc1);
如果你需要进一步阅读,这里有一篇我写的文章