Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a mocking framework like Mockito to mock all relevant Hibernate classes and define expected behavior of these mocks.</p> <p>Sounds like a lot of code, but since the Hibernate Criteria API is a fluent interface, all methods of <code>Criteria</code> return a new instance <code>Criteria</code>. So defining the mock behavior which is common to all tests is simple. Here is an example using Mockito</p> <pre><code>@Mock private SessionFactory sessionFactory; @Mock Session session; @Mock Criteria criteria; CatDao serviceUnderTest; @Before public void before() { reset(sessionFactory, session, criteria); when(sessionFactory.getCurrentSession()).thenReturn(session); when(session.createCriteria(Cat.class)).thenReturn(criteria); when(criteria.setFetchMode(anyString(), (FetchMode) anyObject())).thenReturn(criteria); when(criteria.setFirstResult(anyInt())).thenReturn(criteria); when(criteria.setMaxResults(anyInt())).thenReturn(criteria); when(criteria.createAlias(anyString(), anyString())).thenReturn(criteria); when(criteria.add((Criterion) anyObject())).thenReturn(criteria); serviceUnderTest = new CatDao(sessionFactory); } </code></pre> <p>All methods of the <code>Criteria</code> mock return the mock again.</p> <p>In a concrete test you would then use a <code>ArgumentCaptor</code> and <code>verify</code> statements to investigate what happened to the mocked <code>Criteria</code>.</p> <pre><code>@Test public void testGetCatByName() { ArgumentCaptor&lt;Criterion&gt; captor = ArgumentCaptor.forClass(Criterion.class); serviceUnderTest.getCatByName("Tom"); // ensure a call to criteria.add and record the argument the method call had verify(criteria).add(captor.capture()); Criterion criterion = captor.getValue(); Criterion expectation = Restrictions.eq("name", "Tom"); // toString() because two instances seem never two be equal assertEquals(expectation.toString(), criterion.toString()); } </code></pre> <p>The problem I see with this kind of unitests is that they impose a lot of expectations about the class under test. If you think of <code>serviceUnderTest</code> as a blackbox, you can't know how it retrieves the cat object by name. It could also use a <code>LIKE</code> criterion or even 'IN' instead of <code>=</code>, further it could use the <code>Example</code> criterion. Or it could execute a native SQL query.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload