1+ package com .support .controller ;
2+
3+ import com .support .model .Transaction ;
4+ import com .support .repository .TransactionRepository ;
5+ import org .junit .jupiter .api .Test ;
6+ import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
8+ import org .springframework .boot .test .mock .mockito .MockBean ;
9+ import org .springframework .http .MediaType ;
10+ import org .springframework .test .web .servlet .MockMvc ;
11+
12+ import java .util .Arrays ;
13+ import java .util .Collections ;
14+
15+ import static org .mockito .Mockito .when ;
16+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
17+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
18+
19+ @ WebMvcTest (TransactionController .class )
20+ public class TransactionControllerTest {
21+
22+ @ Autowired
23+ private MockMvc mockMvc ;
24+
25+ @ MockBean
26+ private TransactionRepository repository ;
27+
28+ @ Test
29+ public void shouldReturnAllTransactions () throws Exception {
30+ // Arrange: Create a mock transaction
31+ Transaction t1 = new Transaction ();
32+ t1 .setId (1L );
33+ t1 .setTransactionRef ("REF-123" );
34+ t1 .setStatus ("FAILED" );
35+ t1 .setExposureAmount (500.0 );
36+
37+ when (repository .findAll ()).thenReturn (Arrays .asList (t1 ));
38+
39+ // Act & Assert: Perform the GET request and check the JSON
40+ mockMvc .perform (get ("/api/support/transactions" ))
41+ .andExpect (status ().isOk ())
42+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
43+ .andExpect (jsonPath ("$[0].transactionRef" ).value ("REF-123" ))
44+ .andExpect (jsonPath ("$[0].status" ).value ("FAILED" ))
45+ .andExpect (jsonPath ("$[0].exposureAmount" ).value (500.0 ));
46+ }
47+
48+ @ Test
49+ public void shouldReturnTransactionsByStatus () throws Exception {
50+ // Arrange
51+ Transaction t1 = new Transaction ();
52+ t1 .setStatus ("PROCESSED" );
53+
54+ when (repository .findByStatus ("PROCESSED" )).thenReturn (Arrays .asList (t1 ));
55+
56+ // Act & Assert
57+ mockMvc .perform (get ("/api/support/transactions/status/PROCESSED" ))
58+ .andExpect (status ().isOk ())
59+ .andExpect (jsonPath ("$[0].status" ).value ("PROCESSED" ));
60+ }
61+
62+ @ Test
63+ public void shouldReturnEmptyListWhenNoTransactionsFound () throws Exception {
64+ // Arrange
65+ when (repository .findByStatus ("PENDING" )).thenReturn (Collections .emptyList ());
66+
67+ // Act & Assert
68+ mockMvc .perform (get ("/api/support/transactions/status/PENDING" ))
69+ .andExpect (status ().isOk ())
70+ .andExpect (content ().json ("[]" ));
71+ }
72+ }
0 commit comments