|
| 1 | +package org.baeldung.bddmockito; |
| 2 | + |
| 3 | +import static org.junit.Assert.fail; |
| 4 | +import static org.mockito.BDDMockito.*; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.mockito.Mockito; |
| 10 | +import org.mockito.invocation.InvocationOnMock; |
| 11 | + |
| 12 | + |
| 13 | +public class BDDMockitoTest { |
| 14 | + |
| 15 | + PhoneBookService phoneBookService; |
| 16 | + PhoneBookRepository phoneBookRepository; |
| 17 | + |
| 18 | + String momContactName = "Mom"; |
| 19 | + String momPhoneNumber = "01234"; |
| 20 | + String xContactName = "x"; |
| 21 | + String tooLongPhoneNumber = "01111111111111"; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void init() { |
| 25 | + phoneBookRepository = Mockito.mock(PhoneBookRepository.class); |
| 26 | + phoneBookService = new PhoneBookService(phoneBookRepository); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() { |
| 31 | + given(phoneBookRepository.contains(momContactName)).willReturn(true); |
| 32 | + given(phoneBookRepository.getPhoneNumberByContactName(momContactName)) |
| 33 | + .will((InvocationOnMock invocation) -> { |
| 34 | + if(invocation.getArgument(0).equals(momContactName)) { |
| 35 | + return momPhoneNumber; |
| 36 | + } else { |
| 37 | + return null; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + String phoneNumber = phoneBookService.search(momContactName); |
| 42 | + |
| 43 | + then(phoneBookRepository).should().contains(momContactName); |
| 44 | + then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName); |
| 45 | + Assert.assertEquals(phoneNumber, momPhoneNumber); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenInvalidContactName_whenSearch_thenRetunNull() { |
| 50 | + given(phoneBookRepository.contains(xContactName)).willReturn(false); |
| 51 | + |
| 52 | + String phoneNumber = phoneBookService.search(xContactName); |
| 53 | + |
| 54 | + then(phoneBookRepository).should().contains(xContactName); |
| 55 | + then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName); |
| 56 | + Assert.assertEquals(phoneNumber, null); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() { |
| 61 | + given(phoneBookRepository.contains(momContactName)).willReturn(false); |
| 62 | + |
| 63 | + phoneBookService.register(momContactName, momPhoneNumber); |
| 64 | + |
| 65 | + verify(phoneBookRepository).insert(momContactName, momPhoneNumber); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenEmptyPhoneNumber_whenRegister_thenFail() { |
| 70 | + given(phoneBookRepository.contains(momContactName)).willReturn(false); |
| 71 | + |
| 72 | + phoneBookService.register(xContactName, ""); |
| 73 | + |
| 74 | + then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void givenLongPhoneNumber_whenRegister_thenFail() { |
| 79 | + given(phoneBookRepository.contains(xContactName)).willReturn(false); |
| 80 | + willThrow(new RuntimeException()) |
| 81 | + .given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber)); |
| 82 | + |
| 83 | + try { |
| 84 | + phoneBookService.register(xContactName, tooLongPhoneNumber); |
| 85 | + fail("Should throw exception"); |
| 86 | + } catch (RuntimeException ex) { } |
| 87 | + |
| 88 | + then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenExistentContactName_whenRegister_thenFail() { |
| 93 | + given(phoneBookRepository.contains(momContactName)) |
| 94 | + .willThrow(new RuntimeException("Name already exist")); |
| 95 | + |
| 96 | + try { |
| 97 | + phoneBookService.register(momContactName, momPhoneNumber); |
| 98 | + fail("Should throw exception"); |
| 99 | + } catch(Exception ex) { } |
| 100 | + |
| 101 | + then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments