-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for TagService and SearchService
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
backend/demo-group7/src/test/java/com/group7/demo/SearchServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.group7.demo; | ||
import com.group7.demo.controllers.SearchController; | ||
import com.group7.demo.services.SearchService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@WebMvcTest(SearchController.class) | ||
class SearchServiceTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private SearchService searchService; | ||
|
||
@Test | ||
void testSearch() throws Exception { | ||
// Given | ||
String query = "fitness program"; | ||
Map<String, Object> mockResponse = new HashMap<>(); | ||
mockResponse.put("posts", List.of("Post1", "Post2")); | ||
mockResponse.put("exercises", List.of("Exercise1", "Exercise2")); | ||
mockResponse.put("trainingPrograms", List.of("Program1", "Program2")); | ||
|
||
Mockito.when(searchService.search(eq(query), any(HttpServletRequest.class))).thenReturn(mockResponse); | ||
|
||
// When & Then | ||
mockMvc.perform(get("/api/search") | ||
.param("q", query) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.posts").isArray()) | ||
.andExpect(jsonPath("$.exercises").isArray()) | ||
.andExpect(jsonPath("$.trainingPrograms").isArray()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
backend/demo-group7/src/test/java/com/group7/demo/TagServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.group7.demo; | ||
|
||
import com.group7.demo.models.Tag; | ||
import com.group7.demo.repository.TagRepository; | ||
import com.group7.demo.services.TagService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
class TagServiceTest { | ||
|
||
@Mock | ||
private TagRepository tagRepository; | ||
|
||
private TagService tagService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
tagService = new TagService(tagRepository); | ||
} | ||
|
||
@Test | ||
void testGetAllTagNames() { | ||
// Given | ||
List<Tag> mockTags = Arrays.asList( | ||
Tag.builder().name("Fitness").build(), | ||
Tag.builder().name("Health").build(), | ||
Tag.builder().name("Wellness").build() | ||
); | ||
when(tagRepository.findAll()).thenReturn(mockTags); | ||
|
||
// When | ||
List<String> tagNames = tagService.getAllTagNames(); | ||
|
||
// Then | ||
assertEquals(3, tagNames.size()); | ||
assertEquals(List.of("Fitness", "Health", "Wellness"), tagNames); | ||
} | ||
} |