I do a test for the data removal method.

//MessageController.java @RequestMapping( value = "api/message/{id}", method = RequestMethod.DELETE) public ResponseEntity<?> deleteMessage(@PathVariable Long id) { if (!messageService.delete(id)) { throw new DataNotFoundException("Data with id=" + id + " not found."); } return new ResponseEntity<>(HttpStatus.OK); } //messageServiceImpl.java @Override public boolean delete(Long id) { Message message = messageRepository.findOne(id); if (message == null) { throw new DataNotFoundException("Data with id=" + id + " not found."); } messageRepository.delete(id); return true; } 

The method itself works, records are deleted, I do a test for the service.

 //messageServiceImplTest.java @Mock private MessageService messageService; @InjectMocks MessageServiceImp messageServiceImp; public void delete() throws Exception { //prepare when(messageService.delete(ID)).thenReturn(true); //testing boolean testMessage = messageService.delete(ID); //validate verify(messageService).delete(ID); } 

And I get the error java.lang.Exception: No tests found matching Method delete

There is a method, what is the matter?

  • You are testing the service, but why are you showing the controller code? - Mikhail Vaysman

1 answer 1

You do not have @Test annotation before the test method

  • Thank you, did not notice. The controller method led for sure) - Sergei R
  • @SergeiR why do you have 2 mocks? in theory there should be one mock - MessageService. - Mikhail Vaysman
  • I already rework, I realized that one more, I study the topic of testing. - Sergei R