org.mockito.exceptions.misusing.UnfinishedStubbingException

时间:2015-09-28 08:47:14

标签: junit mockito

在DAO中为getSession方法编写测试类时遇到了一些麻烦 代码

Test class

import org.hibernate.SessionFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.when;


public class SessionDaoImplTest {

private SessionDaoImpl sessionDao;

@Mock
private SessionFactory session;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    sessionDao = new SessionDaoImpl(session);
}

@Test
public void testGetCurrentSession() throws Exception {
     when(sessionDao.getCurrentSession()).
thenReturn(session.getCurrentSession());
}
} 

在测试类

import com.oleg.project.dao.Session.SessionDao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class SessionDaoImpl implements SessionDao {

@Autowired
private SessionFactory sessionFactory;

SessionDaoImpl(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public Session getCurrentSession() {
    Session session = sessionFactory.getCurrentSession();
    return session;
}
}

栈跟踪     org.mockito.exceptions.misusing.UnfinishedStubbingException:     这里检测到未完成的存根:      - >在com.oleg.project.dao.Session.impl。     SessionDaoImplTest.testGetCurrentSession(SessionDaoImplTest.java:28)

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, you naughty developer!
3: you are stubbing the behaviour of another mock inside before     
'thenReturn' instruction if completed

at  com.oleg.project.dao.Session.impl.SessionDaoImplTest.testGetCurrentSession(SessionDaoImplTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

1 个答案:

答案 0 :(得分:0)

变量sessionDao不是模拟。

我认为你的意思是sessionDao = spy(new SessionDaoImpl(session));

修改 您还必须将session.getCurrentSession()提取到局部变量中:

Session currentSession = session.getCurrentSession();
when(sessionDao.getCurrentSession()).thenReturn(currentSession);