Skip to content

Commit

Permalink
Add tests for TagService and SearchService
Browse files Browse the repository at this point in the history
  • Loading branch information
m1u1s1 committed Nov 25, 2024
1 parent f0c4ba7 commit 69cfa21
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
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());
}
}
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);
}
}

0 comments on commit 69cfa21

Please sign in to comment.