13404089107
2026-04-24 e24c81afde61a58d297f4b378fae95fe3385984e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.tcm.training.admin.system.service;
 
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.tcm.training.admin.system.dto.ActivityNoticeCreateRequest;
import com.tcm.training.admin.system.entity.ActivityNotice;
import com.tcm.training.admin.system.event.ActivityNoticeCreatedEvent;
import com.tcm.training.admin.system.mapper.ActivityNoticeMapper;
import com.tcm.training.admin.system.vo.ActivityNoticeListItemVO;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.context.ApplicationEventPublisher;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
import java.time.LocalDateTime;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class ActivityNoticeAppServiceTest {
 
    @BeforeAll
    static void initLambdaCache() {
        MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
        TableInfoHelper.initTableInfo(assistant, ActivityNotice.class);
    }
 
    @Mock
    private ActivityNoticeMapper activityNoticeMapper;
 
    @Mock
    private ActivityNoticeDispatchService activityNoticeDispatchService;
 
    @Mock
    private ApplicationEventPublisher applicationEventPublisher;
 
    @InjectMocks
    private ActivityNoticeAppService activityNoticeAppService;
 
    @Captor
    private ArgumentCaptor<ActivityNotice> activityNoticeCaptor;
 
    @Captor
    private ArgumentCaptor<ActivityNoticeCreatedEvent> eventCaptor;
 
    @Test
    void createShouldTrimTitleAndPersistContent() {
        doAnswer(invocation -> {
            ActivityNotice item = invocation.getArgument(0);
            item.setId(1L);
            return 1;
        }).when(activityNoticeMapper).insert(any(ActivityNotice.class));
 
        ActivityNotice persisted = new ActivityNotice();
        persisted.setId(1L);
        persisted.setTitle("春季活动");
        persisted.setContentHtml("<p>活动内容</p>");
        persisted.setCreatedAt(LocalDateTime.of(2026, 3, 26, 18, 10, 0));
        when(activityNoticeMapper.selectOne(any())).thenReturn(persisted);
 
        ActivityNoticeCreateRequest request = new ActivityNoticeCreateRequest();
        request.setTitle("  春季活动  ");
        request.setContentHtml("<p>活动内容</p>");
 
        ActivityNoticeListItemVO result = activityNoticeAppService.create(request);
 
        verify(activityNoticeMapper).insert(activityNoticeCaptor.capture());
        verify(applicationEventPublisher).publishEvent(eventCaptor.capture());
        assertEquals("春季活动", activityNoticeCaptor.getValue().getTitle());
        assertEquals("<p>活动内容</p>", activityNoticeCaptor.getValue().getContentHtml());
        assertEquals(Long.valueOf(1L), eventCaptor.getValue().getActivityNoticeId());
        assertEquals("春季活动", result.getTitle());
    }
 
    @Test
    void createShouldRejectBlankRichTextContent() {
        ActivityNoticeCreateRequest request = new ActivityNoticeCreateRequest();
        request.setTitle("活动标题");
        request.setContentHtml("<p><br/></p>");
 
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
                () -> activityNoticeAppService.create(request));
 
        assertEquals("请输入通知内容", exception.getMessage());
    }
}