Skip to content

Commit a574ce4

Browse files
committed
Added example for expecting an Exception and added some more expactations to the static mocks
1 parent 243be58 commit a574ce4

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

src/main/java/com/codecentric/sample/store/service/external/ExternalSystemProxy.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.codecentric.sample.store.model.Customer;
44
import org.springframework.stereotype.Service;
55

6+
import java.io.IOException;
7+
68
@Service
79
public class ExternalSystemProxy {
810

@@ -15,4 +17,9 @@ public void update(Customer customer) {
1517
}
1618

1719

20+
public boolean connectionAvailable(String ipAddress) throws IOException {
21+
return true;
22+
}
23+
24+
1825
}

src/main/java/com/codecentric/sample/store/service/external/HostService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.stereotype.Service;
66

7+
import java.io.IOException;
8+
79
@Service
810
public class HostService {
911

12+
@Autowired
13+
private ExternalSystemProxy externalSystemProxy;
14+
1015
public void expand(Customer customer) {
1116

1217
// Host magic happening here to retrieve the value
1318
customer.setHostValue("host value");
1419
}
1520

21+
22+
public void connect(String ipAddress) throws IOException {
23+
24+
if (externalSystemProxy.connectionAvailable(ipAddress)) {
25+
// Do something here
26+
}
27+
}
28+
1629
}

src/test/java/com/codecentric/sample/store/service/CustomerServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public void setUp() throws Exception {
5454
@Test
5555
public void testPLZAddressCombination() {
5656

57-
Customer customer = new Customer("204", "John Do", "224B Bakerstreet");
57+
Customer customer = new Customer("204", "John Do", "221B Bakerstreet");
5858

5959
when(addressService.getPLZForCustomer(customer)).thenReturn(47891);
6060
String address = customerService.getPLZAddressCombination(customer);
6161

62-
assertThat(address, is("47891_224B Bakerstreet"));
62+
assertThat(address, is("47891_221B Bakerstreet"));
6363
}
6464

6565

src/test/java/com/codecentric/sample/store/service/ItemServiceTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
@PrepareForTest({StaticService.class})
3030
public class ItemServiceTest {
3131

32-
3332
@Mock
3433
private ItemRepository itemRepository;
3534

@@ -42,7 +41,6 @@ public void setUp() throws Exception {
4241
MockitoAnnotations.initMocks(this);
4342
}
4443

45-
4644
@Test
4745
public void calculationOfAveragePriceForAllItems() {
4846

@@ -55,7 +53,8 @@ public void calculationOfAveragePriceForAllItems() {
5553
int averagePriceForAllItems = itemService.getAveragePriceForAllItems();
5654

5755
Mockito.verify(itemRepository, times(1)).readAllItems();
58-
verifyStatic();
56+
verifyStatic(times(1));
57+
StaticService.getMultiplicator();
5958

6059
assertThat(averagePriceForAllItems, is(2000*5));
6160
}
@@ -70,7 +69,8 @@ public void readItemDescriptionWithoutIOException() throws IOException {
7069

7170
String value = itemService.readItemDescription(fileName);
7271

73-
verifyStatic();
72+
verifyStatic(times(1));
73+
StaticService.readFile(fileName);
7474

7575
assertThat(value, equalTo("Dummy"));
7676
}
@@ -85,7 +85,8 @@ public void readItemDescriptionWithIOException() throws IOException {
8585

8686
String value = itemService.readItemDescription(fileName);
8787

88-
verifyStatic();
88+
verifyStatic(times(1));
89+
StaticService.readFile(fileName);
8990

9091
assertThat(value, equalTo(""));
9192
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codecentric.sample.store.service.external;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.mockito.InjectMocks;
6+
import org.mockito.Mock;
7+
import org.mockito.MockitoAnnotations;
8+
9+
import java.io.IOException;
10+
11+
import static org.mockito.Mockito.when;
12+
13+
public class HostServiceTest {
14+
15+
@Mock
16+
private ExternalSystemProxy externalSystemProxy;
17+
18+
@InjectMocks
19+
private HostService hostService;
20+
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
MockitoAnnotations.initMocks(this);
25+
}
26+
27+
28+
@Test(expected = IOException.class)
29+
public void testConnectionNotAvailable() throws IOException {
30+
31+
String ipAddress = "10.20.30.40";
32+
33+
when(externalSystemProxy.connectionAvailable(ipAddress)).thenThrow(new IOException());
34+
hostService.connect(ipAddress);
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)