Skip to content

Commit e67392d

Browse files
committed
add java tests
1 parent 079141b commit e67392d

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Support Portal Backend — Java Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
12+
jobs:
13+
build-and-test:
14+
name: Maven Test (Java 17)
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v6
20+
21+
- name: Set up JDK 17
22+
uses: actions/setup-java@v4
23+
with:
24+
java-version: '17'
25+
distribution: 'temurin'
26+
cache: maven
27+
28+
- name: Build and Run Tests
29+
run: |
30+
cd support-portal-backend
31+
mvn clean test
32+
33+
- name: Upload Test Results
34+
if: always()
35+
uses: actions/upload-artifact@v4
36+
with:
37+
name: java-test-results
38+
path: support-portal-backend/target/surefire-reports/
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)