Skip to content

Commit ab40539

Browse files
Merge pull request eugenp#3089 from Namozag/bddmockito
BAEL-1184
2 parents c01c226 + 449c107 commit ab40539

3 files changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.bddmockito;
2+
3+
public interface PhoneBookRepository {
4+
5+
/**
6+
* Insert phone record
7+
* @param name Contact name
8+
* @param phone Phone number
9+
*/
10+
void insert(String name, String phone);
11+
12+
/**
13+
* Search for contact phone number
14+
* @param name Contact name
15+
* @return phone number
16+
*/
17+
String getPhoneNumberByContactName(String name);
18+
19+
/**
20+
* Check if the phonebook contains this contact
21+
* @param name Contact name
22+
* @return true if this contact name exists
23+
*/
24+
boolean contains(String name);
25+
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.baeldung.bddmockito;
2+
3+
public class PhoneBookService {
4+
5+
private PhoneBookRepository phoneBookRepository;
6+
7+
public PhoneBookService(PhoneBookRepository phoneBookRepository) {
8+
this.phoneBookRepository = phoneBookRepository;
9+
}
10+
11+
/**
12+
* Register a contact
13+
* @param name Contact name
14+
* @param phone Phone number
15+
*/
16+
public void register(String name, String phone) {
17+
if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) {
18+
phoneBookRepository.insert(name, phone);
19+
}
20+
}
21+
22+
/**
23+
* Search for a phone number by contact name
24+
* @param name Contact name
25+
* @return Phone number
26+
*/
27+
public String search(String name) {
28+
if(!name.isEmpty() && phoneBookRepository.contains(name)) {
29+
return phoneBookRepository.getPhoneNumberByContactName(name);
30+
}
31+
return null;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)