title: Unit tests for java backend app using jaxrs, dao with jpql, running on glassfish
body: <p>I am thinking about writing JUnits to my CRUD app, but I am wandering is there really a point of having real unit tests. I will show you my basic app structure. In general client app calls REST end point to get list of workers (pagination info included) using service and then dao for accessing database (jpql language). Here is basic structure of it:</p>
  <pre><code>class WorkerRequest {
  
    private int pageSize;
    private int pageNumber;
  
  }
  
  class WorkerController{
  
    @POST
    public Response getWorkers(final WorkerRequest request) {
      return Response.ok(workerService.getWorkers(request)).build();
    }
  
  }
  
  class WorkerService{
  
    public WorkerResponseDTO getWorkers(final WorkerRequest request) {
      final long totalWorkerCount = workerDao.getTotalCount(request);
      final List&lt;WorkerDTO&gt; workers =     
      workerDao.getWorkers(request).stream().map(WorkerDTO::new).collect(toList());
      return new WorkerResponseDTO(totalWorkerCount, workers);
    }
  
  }
  
  
  class WorkerDao implements BasicWorkerDao{
  
    @Override
    public List&lt;Worker&gt; getWorkersForPage(final WorkerRequest request) {
      final String query = &quot;select w from Worker w&quot;;
      return entityManager.createQuery(query, Worker.class)
          .setFirstResult(util.calcStartPosition(request))
          .setMaxResults(request.getPageSize())
          .getResultList();
    }
  
    @Override
    public long getTotalWorkerCount(final WorkerRequest request) {
      return entityManager.createQuery(&quot;select count(w) from Worker w&quot;, Long.class).getSingleResult();
    }
  }
  </code></pre>
  <p>Now I am thinking, it there really a point for me to introduce real unit tests? I mean I can mock my real dao with some MockDao and in there I can simply introduce arraylist of workers. But how this would work?</p>
  <p>For example in real scenario if I pass a request with pageSize=20, and pageNumber=4 I want workers from 61 to 80, that means I will simply write in my MockDao in getWorkersForPage code like list.stream().skip((pageNumber-1)*pageSize).limit(pageSize), and that is all? It seems silly for me.</p>
  <p>Maybe if dao was responsible for getting all workers always, and service would then, filter it, did some sorting, calculate pagination etc than it would make sense to have those unit tests. But when SQL is doing all the job is it wort it to use this MockDao ?</p>
  <p>Or maybe the only way to test this feature/app is to provide a integration tests with some memory db, and then test would make sense, since I would test sql statements as well.</p>
  <p>I hope you know what I mean, what is usually your approach, what is the best approach in this case?</p>
  <p>cheers</p>