Mockito: How to check my service with a mockery?

I am new to testing.

I want to check my CorrectionService.CorrectPerson(Long personId) method. The implementation has not been written yet, but this is what it will do:

CorrectionService will call the AddressDAO method, which will remove the Adress part that has a Person . One Person has many Address es

I'm not sure if the main structure should be my CorrectionServiceTest.testCorrectPerson .

Also, please do / do not confirm that in this test I do not need to check if the addresses are really deleted (should be done in AddressDaoTest ), only that the DAO method was called.

thanks

+4
source share
2 answers

A simplified version of the CorrectionService class (visibility modifiers removed for simplicity).

 class CorrectionService { AddressDao addressDao; CorrectionService(AddressDao addressDao) { this.addressDao; } void correctPerson(Long personId) { //Do some stuff with the addressDao here... } } 

In your test:

 import static org.mockito.Mockito.*; public class CorrectionServiceTest { @Before public void setUp() { addressDao = mock(AddressDao.class); correctionService = new CorrectionService(addressDao); } @Test public void shouldCallDeleteAddress() { correctionService.correct(VALID_ID); verify(addressDao).deleteAddress(VALID_ID); } } 
+3
source

Pure version:

 @RunWith(MockitoJUnitRunner.class) public class CorrectionServiceTest { private static final Long VALID_ID = 123L; @Mock AddressDao addressDao; @InjectMocks private CorrectionService correctionService; @Test public void shouldCallDeleteAddress() { //when correctionService.correct(VALID_ID); //then verify(addressDao).deleteAddress(VALID_ID); } } 
+11
source

Source: https://habr.com/ru/post/1301690/


All Articles