title: Spring Data: Service layer unit testing
body: <p>In my project I'm having trouble doing unit testing. One issue is that just doing an integration test is much faster to write and also tests that the components actually work together. Unit testing novel "algorithms" or so seems much easier. Unit Testing service classes it just feels wrong and useless. </p>
  
  <p>I'm using mockito to mock spring data repository (and hence DB access). The thing is if i tell the mocked repository to return entity A on method call getById it will obviously return that and the service will return it too. Yes, the service does some extra stuff, but very minor things, like load lazy collections (from hibernate). Obviously I don't have any lazy collections (proxies) in a unit test.</p>
  
  <p>Example:</p>
  
  <pre><code>@Test
  public void testGetById() {
      System.out.println("getById");
      TestCompound expResult = new TestCompound(id, "Test Compound", "9999-99-9", null, null, null);
  
      TestCompoundRepository mockedRepository = mock(TestCompoundRepository.class);
      when(mockedRepository.findOne(id)).thenReturn(expResult);
  
      ReflectionTestUtils.setField(testCompoundService, "testCompoundRepository",
              mockedRepository, TestCompoundRepository.class);
  
      TestCompound result = testCompoundService.getById(id);
      assertEquals(expResult, result);
  }
  </code></pre>
  
  <p>hooray, the rest succeeds. What a surprise! Not really no. </p>
  
  <p>Can some one explain to me what I'm doing wrong? Or else what the point of such a test is? I mean I tell to return expResult and then it is returned. Wow. What a surprise! Feels like I'm testing if mockito works and not my Service.</p>
  
  <p>EDIT:</p>
  
  <p>The only benefit I see if some were stupid error happens like leaving an unwanted line there that sets return value to null or something similar stupid. Such cases would be caught by the unit test. Still the "reward-effort" ratio seems bad?</p>