Merge pull request #366 from nagisa77/codex/fix-userrepository-bean-not-found-error

Fix TagControllerTest dependency setup
This commit is contained in:
Tim
2025-08-04 21:32:06 +08:00
committed by GitHub

View File

@@ -1,6 +1,9 @@
package com.openisle.controller; package com.openisle.controller;
import com.openisle.mapper.TagMapper;
import com.openisle.mapper.PostMapper;
import com.openisle.model.Tag; import com.openisle.model.Tag;
import com.openisle.repository.UserRepository;
import com.openisle.service.TagService; import com.openisle.service.TagService;
import com.openisle.service.PostService; import com.openisle.service.PostService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -9,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
@@ -21,6 +25,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest(TagController.class) @WebMvcTest(TagController.class)
@AutoConfigureMockMvc(addFilters = false) @AutoConfigureMockMvc(addFilters = false)
@Import(TagMapper.class)
class TagControllerTest { class TagControllerTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@@ -31,6 +36,12 @@ class TagControllerTest {
@MockBean @MockBean
private PostService postService; private PostService postService;
@MockBean
private UserRepository userRepository;
@MockBean
private PostMapper postMapper;
@Test @Test
void createAndGetTag() throws Exception { void createAndGetTag() throws Exception {
Tag t = new Tag(); Tag t = new Tag();
@@ -64,7 +75,7 @@ class TagControllerTest {
t.setDescription("d2"); t.setDescription("d2");
t.setIcon("i2"); t.setIcon("i2");
t.setSmallIcon("s2"); t.setSmallIcon("s2");
Mockito.when(tagService.listTags()).thenReturn(List.of(t)); Mockito.when(tagService.searchTags(null)).thenReturn(List.of(t));
mockMvc.perform(get("/api/tags")) mockMvc.perform(get("/api/tags"))
.andExpect(status().isOk()) .andExpect(status().isOk())