title: Spring + QueryDsl + Mockito: How to write a unit test case for a simple function
body: <p>I have the following function in my service.</p>
  
  <pre><code>public boolean checkNameUnique(String name) {
      QEntity qEntity = QEntity.entity;
      BooleanExpression nameUniquePredicate = qEntity.name.eq(name);
      long count = entityReadRepository.count(nameUniquePredicate);
      return count == 0;
  }
  </code></pre>
  
  <p>It just checks if the name already exists in db. That needs to be unique, so it returns true if does not already exist and false if it does. </p>
  
  <p>Now how do I write a mockito unit test case for this? I am new to Mockito and writing unit test cases, hence the question. </p>
  
  <p>My reading on Mockito has lead me to write something on the lines of</p>
  
  <pre><code>when(entityReadRepository.count(nameUniquePredicate)).thenReturn(1);
  </code></pre>
  
  <p>and then call the function to be tested. But that doesn't make any sense.</p>
  
  <p><code>Entity</code> is Hibernate entity which corresponds to a table in the DB
  <code>entityReadRepository</code> extends <code>JpaRepository</code> and <code>QueryDslPredicateExecutor</code>. <code>QEntity</code> is the <code>Q</code> object generated by <code>QueryDsl</code>'s plugin. </p>