--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
Thank you very much for these suggestions. I may still be doing something wrong. Both these fail for me, and for different reasons.
Eclipse insists that I cast session thus, which is something nasty I've read about out there in Google land and never found an answer to. I don't know the difference between org.hibernate.Session, which I'm using in my code, and the "classic" one, which I didn't even know about until I began trying to work with Hibernate and Mockito in the same breath:
@Mock SessionFactory sessionFactory;
@Mock Session session; ----added this
@Test
public void create_should_call_save()
{
when( sessionFactory.getCurrentSession() ).thenReturn( ( org.hibernate.classic.Session ) session );verify( this.session ).save( any() );
Language result = this.dao.create( ENGLISH );
assertNotNull( result );
assertTrue( checkLanguageResult( ENGLISH, result ) );
}
I get this result:
java.lang.ClassCastException: org.hibernate.Session$$EnhancerByMockitoWithCGLIB$$a94debe1 cannot be cast to org.hibernate.classic.Session
at com.hp.web.user.entity.LanguageRepositoryTest.create_should_call_save(LanguageRepositoryTest.java:61)...
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Anyway, no, I don't think I do wish to call session.save() since I'm trying to eliminate Hibernate from the equation by mocking/stubbing/shutting it up. (If I'm mistaken in the way I look at this, feel free to tell me.)
Next, not calling it, yields the same complaint I wrote for:
@Test
public void create_dont_call_save()doNothing().when( session ).save( any() );
{
Language ENGLISH = mock( Language.class );
verify( this.dao).create( ENGLISH );
Language result = this.dao.create( ENGLISH );
assertNotNull( result );
assertTrue( checkLanguageResult( ENGLISH, result ) );
}
yields:
org.mockito.exceptions.misusing.UnfinishedStubbingException:-> at com.hp.web.user.entity.LanguageRepositoryTest.create_should_call_save(LanguageRepositoryTest.java:61)
Unfinished stubbing detected here:
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!etc.
Russ
In our case this has been very helpful, since Hibernate is full of bugs
and limitations when you move out of the non-trivial. By having
Hibernate actually run the SQL statement against a database, rather than
just verify that the Hibernate session has been passed some criteria or
query, we are able to catch the bugs up front. This also helps catch
problems with your mappings/annotations.
Sure, there may be variations between hsqldb and your production
database, both in SQL support and Hibernate translation, so this isn't
*istead of* integration tests, but those cases are rare in comparison to
the cases where hsqldb really is helpful.
</Mattias>
----- Original Message -----
Subject: [mockito] Mocking/stubbing stuff near the "bottom of the pile"...
Date: Fri, 16 Mar 2012 10:58:37 -0600
From: Russell Bateman <ru...@windofkeltia.com>
I know the caveat about mocking/stubbing stuff one doesn't own, in this
case, Hibernate. And yet, can I get some guidance on how to write JUnit
tests for my DAO layer? ...
I know the caveat about mocking/stubbing stuff one doesn't own
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+unsubscribe@googlegroups.com.
These are effectively integration tests, because they're testing that your
DAO integrates correctly to Hibernate. To get your integration tests to be
efficient, you really want an in-memory database, as Mattias has suggested.
My favourite is H2 (http://www.h2database.com/).
Hope this helps,
David.
</Mattias>
--
You received this message because you are subscribed to the Google Groups
"mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to
mockito+u...@googlegroups.com.
yeah, that would be my recommendation too, to use hsqldb and run directly with hibernate.
Since touching a real database is not a unit test anymore, personally I prefer not to do just "hibernate tests" if not necessary. They used to be included in larger integration test suites with webdriver or similar, when we're verifying whole features and not just a part of the system.
To unsubscribe from this group, send email to mockito+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+unsubscribe@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.