-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPaymentControllerTest.java
More file actions
90 lines (75 loc) · 3.16 KB
/
PaymentControllerTest.java
File metadata and controls
90 lines (75 loc) · 3.16 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
package com.bravo.user.controller;
import com.bravo.user.App;
import com.bravo.user.model.dto.PaymentDto;
import com.bravo.user.service.PaymentService;
import com.bravo.user.utility.PageUtil;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ContextConfiguration(classes = {App.class})
@ExtendWith(SpringExtension.class)
@SpringBootTest()
@AutoConfigureMockMvc
public class PaymentControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private PaymentService paymentService;
private List<PaymentDto> payments;
@BeforeEach
public void beforeEach(){
final List<Integer> ids = IntStream
.range(1, 10)
.boxed()
.collect(Collectors.toList());
this.payments = ids.stream()
.map(id -> createPaymentDto(Integer.toString(id)))
.collect(Collectors.toList());
}
@Test
void getRetrieve() throws Exception {
when(paymentService
.retrieveByUserId(anyString(), any(PageRequest.class), any(HttpServletResponse.class)))
.thenReturn(payments);
final ResultActions result = this.mockMvc
.perform(get("/payment/testid/retrieve"))
.andExpect(status().isOk());
for(int i = 0; i < payments.size(); i++){
result.andExpect(jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId()));
}
final PageRequest pageRequest = PageUtil.createPageRequest(null, null);
verify(paymentService).retrieveByUserId(
eq("testid"), eq(pageRequest), any(HttpServletResponse.class)
);
}
@Test
void getRetrieveWithIdMissing() throws Exception {
this.mockMvc.perform(get("/payment/retrieve/"))
.andExpect(status().isNotFound());
}
private PaymentDto createPaymentDto(final String id){
final PaymentDto payment = new PaymentDto();
payment.setId(id);
payment.setCardNumberLast4("0000");
return payment;
}
}