I have an issue while mocking one of interface values. When I am running test in debug mode it is working fine but while run I have org.mockito.exceptions.misusing.UnfinishedStubbingException.
Update 16.08.2018: it's working only when I set up breakpoint on broken line or before - without it test crashes also on debug. And if I hold F9 all the time some test passes and some fail so it have to be connected with time of inicialization but still no idea how to fix it
On debug mode it passes but on run mode I got exception in Service while executing: String name = getPhoneBookName();
I also try to move Mockito.when to setup block but it doesn't help
Any ideas?
Code looks like:
MyServiceSpec:
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()
Service:
someMethod() {
String name = getPhoneBookName();
if (name != null) {
map.put("remotePhoneBookName", name);
}
}
@Override
public String getPhoneBookName() {
return propertyDAO.getValue(PropertyDAO.Key.REMOTE_BOOK_NAME);
}
PropertyDao
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;
}
Ok, I got it.
It turns out that in my test below critical mock I try to mock someMethod() from the question and then it crash on execution. Thank You very much @Michał Piątkowski because at the end You was right ;)
It works when I replace :
when(propertyDAO.getValue(PropertyDAO.Key.REMOTE_BOOK_NAME)).thenReturn("Phonebook")
when(configuration.getStationValues(any(), any(), anyString())).thenReturn(someMethod())
to
when(propertyDAO.getValue(PropertyDAO.Key.REMOTE_BOOK_NAME)).thenReturn("Phonebook")
def method =someMethod()
when(configuration.getStationValues(any(), any(), anyString())).thenReturn(method)
You can feel code smell even in next building, but I do not have idea about another workaround for now
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With