mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-03 08:27:35 +08:00
test: mock NotificationMapper in NotificationControllerTest
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package com.openisle.controller;
|
package com.openisle.controller;
|
||||||
|
|
||||||
|
import com.openisle.dto.NotificationDto;
|
||||||
|
import com.openisle.dto.PostSummaryDto;
|
||||||
|
import com.openisle.mapper.NotificationMapper;
|
||||||
import com.openisle.model.Notification;
|
import com.openisle.model.Notification;
|
||||||
import com.openisle.model.NotificationType;
|
import com.openisle.model.NotificationType;
|
||||||
import com.openisle.model.Post;
|
import com.openisle.model.Post;
|
||||||
import com.openisle.service.NotificationService;
|
import com.openisle.service.NotificationService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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;
|
||||||
@@ -31,6 +33,9 @@ class NotificationControllerTest {
|
|||||||
@MockBean
|
@MockBean
|
||||||
private NotificationService notificationService;
|
private NotificationService notificationService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private NotificationMapper notificationMapper;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void listNotifications() throws Exception {
|
void listNotifications() throws Exception {
|
||||||
Notification n = new Notification();
|
Notification n = new Notification();
|
||||||
@@ -40,9 +45,16 @@ class NotificationControllerTest {
|
|||||||
p.setId(2L);
|
p.setId(2L);
|
||||||
n.setPost(p);
|
n.setPost(p);
|
||||||
n.setCreatedAt(LocalDateTime.now());
|
n.setCreatedAt(LocalDateTime.now());
|
||||||
Mockito.when(notificationService.listNotifications("alice", null))
|
when(notificationService.listNotifications("alice", null))
|
||||||
.thenReturn(List.of(n));
|
.thenReturn(List.of(n));
|
||||||
|
|
||||||
|
NotificationDto dto = new NotificationDto();
|
||||||
|
dto.setId(1L);
|
||||||
|
PostSummaryDto ps = new PostSummaryDto();
|
||||||
|
ps.setId(2L);
|
||||||
|
dto.setPost(ps);
|
||||||
|
when(notificationMapper.toDto(n)).thenReturn(dto);
|
||||||
|
|
||||||
mockMvc.perform(get("/api/notifications")
|
mockMvc.perform(get("/api/notifications")
|
||||||
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
@@ -63,7 +75,7 @@ class NotificationControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void unreadCountEndpoint() throws Exception {
|
void unreadCountEndpoint() throws Exception {
|
||||||
Mockito.when(notificationService.countUnread("alice")).thenReturn(3L);
|
when(notificationService.countUnread("alice")).thenReturn(3L);
|
||||||
|
|
||||||
mockMvc.perform(get("/api/notifications/unread-count")
|
mockMvc.perform(get("/api/notifications/unread-count")
|
||||||
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
||||||
|
|||||||
Reference in New Issue
Block a user