-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathAddressServiceTest.java
More file actions
92 lines (79 loc) · 3.68 KB
/
AddressServiceTest.java
File metadata and controls
92 lines (79 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.bravo.user.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.bravo.user.App;
import com.bravo.user.dao.model.Address;
import com.bravo.user.dao.model.mapper.ResourceMapper;
import com.bravo.user.dao.repository.AddressRepository;
import com.bravo.user.model.dto.AddressDto;
@ContextConfiguration(classes = { App.class })
@ExtendWith(SpringExtension.class)
@SpringBootTest
class AddressServiceTest {
//I looked at tests that have already been written to try to get a better understanding
//of the different components of this app
//autowired ? - allows you to inject the object dependency implicitly
@Autowired
private AddressService addressService;
//mock bean? this will replace any existing bean of the same type in the application context
//according to what I have read, you can also use a @Qualifier notation and pass in qualifier metadata
//mock resource mapper to use in tests
@MockBean
private ResourceMapper resourceMapper;
//mock address repo to use in tests
@MockBean
private AddressRepository addressRepository;
//empty (?) list of data transfer object - addresses
private List<AddressDto> dtoAddresses;
@BeforeEach
public void beforeEach() {
/* wow! this is my first time learning about the Collectors class - pretty cool!
-allows us to accumulate/reduce values into a collection
IntStream.range()? - sequence of primitive int values that fall between given range, .boxed() - each element boxed to an Integer
*/
final List<Integer> ids = IntStream.range(1, 10).boxed().collect(Collectors.toList());
//here we use stream() to save a list of addresses
final List<Address> daoAddresses = ids.stream()
.map(id -> createAddress(Integer.toString(id))).collect(Collectors.toList());
//I think what is happening now is the id's are being mapped to the daoAddresses list
//when keyword - Mockito class! also new to me - sent down a new rabbit hole
//Mockito is a mocking framework for unit tests in Java - allows the creation of test double objects in automated unit tests
//"used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in testing"
when(addressRepository.findByUserId(anyString())).thenReturn(daoAddresses);
//this keyword refers to this specific instance
this.dtoAddresses = ids.stream().map(id -> createAddressDto(Integer.toString(id)))
.collect(Collectors.toList());
when(resourceMapper.convertAddresses(daoAddresses)).thenReturn(dtoAddresses);
}
//testing that endpoint retrieves correct id value
@Test
void retrieveByUserId() {
final String userId = "123a-456b";
final List<AddressDto> results = addressService.retrieveByUserId(userId);
assertEquals(dtoAddresses, results);
verify(addressRepository).findByUserId(userId);
}
private Address createAddress(final String id) {
final Address address = new Address();
address.setId(id);
return address;
}
private AddressDto createAddressDto(final String id) {
final AddressDto address = new AddressDto();
address.setId(id);
return address;
}
}