@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder([Link])
public class FitnessTrackerApplicationTests {
@InjectMocks
private FitnessController fitnessController;
@Mock
private GoalService goalService;
@Mock
private ActivityService activityService;
@Mock
private ChallengeService challengeService;
@Mock
private FitnessGoalRepository goalRepository;
@Mock
private ActivityLogRepository activityLogRepository;
@Mock
private ChallengeRepository challengeRepository;
@Autowired
private MockMvc mockMvc;
private final ObjectMapper objectMapper = new ObjectMapper();
@BeforeEach
public void setUp() {
[Link](this);
mockMvc = [Link](fitnessController).build();
}
// Controller Test Cases
@Test
@Order(1)
public void testCreateGoal() throws Exception {
GoalDTO goalDTO = new GoalDTO( "Weight Loss", 5.0, [Link](),
[Link]().plusDays(30));
[Link](post("/api/goals")
.contentType(MediaType.APPLICATION_JSON)
.content([Link](goalDTO)))
.andExpect(status().isOk())
.andExpect(content().string("Goal created successfully!"));
verify(goalService, times(1)).createGoal(any([Link]));
}
@Test
@Order(2)
public void testGetAllGoals() throws Exception {
List<GoalDTO> goals = [Link](
new GoalDTO( "Weight Loss", 5.0, [Link](),
[Link]().plusDays(30)),
new GoalDTO( "Muscle Gain", 10.0, [Link](),
[Link]().plusDays(60)));
when([Link]()).thenReturn(goals);
[Link](get("/api/goals"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2));
verify(goalService, times(1)).getAllGoals();
}
@Test
@Order(3)
public void testGetGoalById() throws Exception {
GoalDTO goalDTO = new GoalDTO( "Weight Loss", 5.0, [Link](),
[Link]().plusDays(30));
when([Link](1L)).thenReturn(goalDTO);
[Link](get("/api/goals/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.goalType").value("Weight Loss"));
verify(goalService, times(1)).getGoalById(1L);
}
@Test
@Order(4)
public void testUpdateGoal() throws Exception {
GoalDTO goalDTO = new GoalDTO( "Weight Loss", 7.0, [Link](),
[Link]().plusDays(30));
[Link](put("/api/goals/1")
.contentType(MediaType.APPLICATION_JSON)
.content([Link](goalDTO)))
.andExpect(status().isOk())
.andExpect(content().string("Goal updated successfully!"));
verify(goalService, times(1)).updateGoal(eq(1L), any([Link]));
}
@Test
@Order(5)
public void testDeleteGoal() throws Exception {
[Link](delete("/api/goals/1"))
.andExpect(status().isOk())
.andExpect(content().string("Goal deleted successfully!"));
verify(goalService, times(1)).deleteGoal(1L);
}
@Test
@Order(6)
public void testAddActivityLog() throws Exception {
ActivityLogDTO activityLogDTO = new ActivityLogDTO( "Running", 30d,
[Link]());
[Link](post("/api/activities")
.contentType(MediaType.APPLICATION_JSON)
.content([Link](activityLogDTO)))
.andExpect(status().isOk())
.andExpect(content().string("Activity log added successfully!"));
verify(activityService,
times(1)).addActivityLog(any([Link]));
}
@Test
@Order(7)
public void testGetAllActivityLogs() throws Exception {
List<ActivityLogDTO> activityLogs = [Link](
new ActivityLogDTO( "Running", 30d, [Link]()),
new ActivityLogDTO( "Cycling", 45d, [Link]()));
when([Link]()).thenReturn(activityLogs);
[Link](get("/api/activities"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2));
verify(activityService, times(1)).getAllActivityLogs();
}
@Test
@Order(8)
public void testGetProgressAnalytics() throws Exception {
List<ProgressDTO> progress = [Link](
new ProgressDTO("Weight Loss", 3.0, "In Progress"),
new ProgressDTO("Muscle Gain", 8.0, "Completed"));
when([Link]()).thenReturn(progress);
[Link](get("/api/progress"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2));
verify(goalService, times(1)).getProgressAnalytics();
}
@Test
@Order(9)
public void testGetAllChallenges() throws Exception {
List<ChallengeDTO> challenges = [Link](
new ChallengeDTO(),
new ChallengeDTO( "Run 5K Challenge", "Run 5 kilometers in a single
session", false));
when([Link]()).thenReturn(challenges);
[Link](get("/api/challenges"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(2));
verify(challengeService, times(1)).getAllChallenges();
}
@Test
@Order(10)
public void testParticipateInChallenge() throws Exception {
[Link](post("/api/challenges/participate/1"))
.andExpect(status().isOk())
.andExpect(content().string("Successfully joined the challenge!"));
verify(challengeService, times(1)).participateInChallenge(1L);
}
@Test
@Order(11)
public void testGetChallengeDetails() throws Exception {
ChallengeDTO challengeDTO = new ChallengeDTO(1L, "10K Steps Challenge",
"Walk 10,000 steps every day for 30 days", true);
when([Link](1L)).thenReturn(challengeDTO);
[Link](get("/api/challenges/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title").value("10K Steps Challenge"));
verify(challengeService, times(1)).getChallengeDetails(1L);
}
// Service Test Cases
@Test
@Order(12)
public void testGoalServiceCreateGoal() {
GoalDTO goalDTO = new GoalDTO( "Weight Loss", 5.0, [Link](),
[Link]().plusDays(30));
[Link](goalDTO);
verify(goalRepository, times(1)).save(any([Link]));
}
@Test
@Order(13)
public void testGoalServiceGetAllGoals() {
List<FitnessGoal> goals = [Link](
new FitnessGoal( "Weight Loss", 5.0d, [Link](),
[Link]().plusDays(30)),
new FitnessGoal( "Muscle Gain", 10.0d, [Link](),
[Link]().plusDays(60)));
when([Link]()).thenReturn(goals);
List<GoalDTO> result = [Link]();
assertEquals(2, [Link]());
verify(goalRepository, times(1)).findAll();
}
@Test
@Order(14)
public void testGoalServiceGetGoalById() {
Goal goal = new Goal(1L, "Weight Loss", 5.0, [Link](),
[Link]().plusDays(30));
when([Link](1L)).thenReturn([Link](goal));
GoalDTO result = [Link](1L);
assertNotNull(result);
assertEquals("Weight Loss", [Link]());
}
@Test
@Order(15)
public void testGoalServiceDeleteGoal() {
Goal goal = new Goal(1L, "Weight Loss", 5.0, [Link](),
[Link]().plusDays(30));
when([Link](1L)).thenReturn([Link](goal));
[Link](1L);
verify(goalRepository, times(1)).delete(goal);
}
// Additional Test Cases for Other Layers and Models would follow this pattern.
// Add similar test cases for ActivityService, ChallengeService, and their
repositories.