title: SpringBoot: Testing the Service layer
body: <p>Let's assume that I have two classes:</p>
  
  <ol>
  <li>TodoRepository</li>
  <li>TodoService</li>
  </ol>
  
  <p>the <code>TodoRepository</code> is a simple CRUD Repository:</p>
  
  <pre><code>public interface TodoRepository extends CrudRepository&lt;T, ID&gt; {
  }
  </code></pre>
  
  <p>the <code>TodoService</code> is just a class which calls this Repository:</p>
  
  <pre><code>@Service
  public class TodoService{
  
      private final TodoRepository todoRepository;
  
      @Autowired
      public TodoService(TodoRepository todoRepository) {
          this.todoRepository = todoRepository;
      }
  
      public void createTodo(Todo todo) {
          todoRepository.save(todo);
      }
  }
  </code></pre>
  
  <p>Should I bother testing the service layer?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Thanks to the explanation from @Dherik. I created a test class which looks like:</p>
  
  <p><em>Note:</em> I am using JUnit5, Mockito and Spring framework</p>
  
  <pre><code>@ExtendWith(SpringExtension.class)
  class TodoServiceTest {
  
      @MockBean
      private TodoRepository todoRepository;
  
      private TodoService todoService;
  
      @BeforeEach
      void setUp() {
          todoService = new TodoService(todoRepository);
      }
  
      @AfterEach
      void tearDown() {
          clearInvocations(tanklevelRepository);
      }
  
      @Test
      public void createTodo() {
          todoService.createTodo(new Todo());
  
          // verify if the save method is called when createTodo is called too
          verify(todoRepository, times(1)).save(any(Todo.class));
      }
  }
  </code></pre>