【发布时间】:2019-01-21 02:04:09
【问题描述】:
我在模拟其中一个接口值时遇到问题。当我在调试模式下运行测试时,它工作正常,但在运行时我有org.mockito.exceptions.misusing.UnfinishedStubbingException。
更新 16.08.2018:只有当我在断线或之前设置断点时它才有效 - 没有它在调试时也会测试崩溃。如果我一直按住 F9,有些测试通过,有些失败,所以它必须与初始化时间联系起来,但仍然不知道如何修复它
在调试模式下它通过但在运行模式下我在执行时在服务中遇到异常:String name = getPhoneBookName();
我也尝试将 Mockito.when 移动到设置块,但没有帮助
有什么想法吗?
代码如下:
我的服务规范:
private PropertyDAO propertyDAO = mock(PropertyDAO.class)
def "Should fill all fields property for existing user"() {
given:"mock values to check proper form"
when(propertyDAO.getValue(PropertyDAO.Key.REMOTE_BOOK_NAME)).thenReturn("Phonebook")
when:
service.someMethod()
服务:
someMethod() {
String name = getPhoneBookName();
if (name != null) {
map.put("remotePhoneBookName", name);
}
}
@Override
public String getPhoneBookName() {
return propertyDAO.getValue(PropertyDAO.Key.REMOTE_BOOK_NAME);
}
物业道
public interface PropertyDAO {
String VERSION_KEY = "VERSION";
public enum Key {
VERSION,
REMOTE_BOOK_NAME
}
String getValue(Key key);
PropertyHibernateDAO
@Repository("propertyDAO")
@Qualifier("hibernate")
public class PropertyHibernateDAO implements PropertyDAO {
@Override
@Transactional(readOnly = true)
public String getValue(Key key) {
String result = null;
Session session = sessionFactory.getCurrentSession();
String dbKey = DB_KEYS.get(key);
Criteria criteria = session.createCriteria(Property.class);
criteria.add(Restrictions.eq("key", dbKey));
Property property = (Property) criteria.uniqueResult();
if (property != null) {
result = property.getValue();
}
return result;
}
【问题讨论】:
-
我不这么认为,因为在我的情况下,我尝试返回简单文本而不是任何变量。但我又发现了一个问题:主要问题是对 propertyDao 的模拟。在我的由服务(ConfigurationImpl.class)自动装配的类中,我还有一个使用它的地方: switch (propertyDAO.getAcsConfig().getLang()) { someLogic } 而且它也在运行时崩溃而不是在调试时崩溃:/我认为这可能是该类 configurationImpl.init(propertyDAO, mock(PhoneModelDAO.class)) 的初始化引起的
标签: java unit-testing mockito tdd spock