Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
practical_train
Commits
8f8b9d12
Commit
8f8b9d12
authored
Aug 11, 2026
by
wujipeng
Browse files
优化
parent
6c687b75
Changes
1
Hide whitespace changes
Inline
Side-by-side
practical-business/src/test/java/com/platform/business/service/impl/NotificationMessageServiceImplTest.java
deleted
100644 → 0
View file @
6c687b75
package
com.platform.business.service.impl
;
import
com.alibaba.fastjson2.JSON
;
import
com.platform.business.mapper.NotificationMessageMapper
;
import
com.platform.business.mapper.NotificationMessageRecipientMapper
;
import
com.platform.business.mapper.NotificationOutboxMapper
;
import
com.platform.business.model.dto.NotificationEventDTO
;
import
com.platform.business.model.dto.NotificationPublishDTO
;
import
com.platform.business.model.dto.NotificationPushDTO
;
import
com.platform.business.model.entity.NotificationMessageEntity
;
import
com.platform.business.model.entity.NotificationOutboxEntity
;
import
com.platform.business.service.BusinessNotificationPublisher
;
import
com.platform.business.service.NotificationAudienceService
;
import
com.platform.business.service.NotificationPushDispatcher
;
import
com.platform.business.service.NotificationUnreadCache
;
import
com.platform.exception.BusinessException
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.springframework.transaction.support.TransactionSynchronization
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Date
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertNotNull
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyList
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verifyNoInteractions
;
import
static
org
.
mockito
.
Mockito
.
when
;
/** 消息中心核心服务单元测试,不依赖真实MySQL、RabbitMQ或Redis。 */
@ExtendWith
(
MockitoExtension
.
class
)
class
NotificationMessageServiceImplTest
{
@Mock
private
NotificationMessageMapper
messageMapper
;
@Mock
private
NotificationMessageRecipientMapper
recipientMapper
;
@Mock
private
NotificationOutboxMapper
outboxMapper
;
@Mock
private
NotificationAudienceService
audienceService
;
@Mock
private
NotificationPushDispatcher
pushDispatcher
;
@Mock
private
NotificationUnreadCache
unreadCache
;
@InjectMocks
private
NotificationMessageServiceImpl
service
;
/** 验证发布请求只写入包含完整事件快照的事务发件箱。 */
@Test
void
publishWritesCompleteTransactionalOutboxEvent
()
{
NotificationPublishDTO
request
=
new
NotificationPublishDTO
();
request
.
setMessageType
(
"EXAM"
);
request
.
setTitle
(
"考试即将开始"
);
request
.
setContent
(
"请提前十分钟进入考场"
);
request
.
setRecipientUserIds
(
Collections
.
singletonList
(
"user-1"
));
// 业务模块仅依赖单方法发布接口,不需要感知消息中心的查询和状态管理能力。
BusinessNotificationPublisher
publisher
=
service
;
String
eventId
=
publisher
.
publish
(
request
,
"admin-1"
,
"管理员"
);
ArgumentCaptor
<
NotificationOutboxEntity
>
captor
=
ArgumentCaptor
.
forClass
(
NotificationOutboxEntity
.
class
);
verify
(
outboxMapper
).
insert
(
captor
.
capture
());
NotificationOutboxEntity
outbox
=
captor
.
getValue
();
NotificationEventDTO
event
=
JSON
.
parseObject
(
outbox
.
getPayload
(),
NotificationEventDTO
.
class
);
assertEquals
(
eventId
,
outbox
.
getEventId
());
assertEquals
(
"notification.exam"
,
outbox
.
getRoutingKey
());
assertEquals
(
"EXAM"
,
event
.
getMessageType
());
assertEquals
(
"admin-1"
,
event
.
getSenderId
());
assertNotNull
(
outbox
.
getScheduledAt
());
}
/** 验证没有任何接收范围的消息在写库前被拒绝。 */
@Test
void
publishRejectsRequestWithoutAudience
()
{
NotificationPublishDTO
request
=
new
NotificationPublishDTO
();
request
.
setMessageType
(
"SYSTEM"
);
request
.
setTitle
(
"系统通知"
);
request
.
setContent
(
"内容"
);
assertThrows
(
BusinessException
.
class
,
()
->
service
.
publish
(
request
,
"admin-1"
,
"管理员"
));
}
/** 验证消费者先持久化接收人,再构造在线推送并使未读缓存失效。 */
@Test
void
consumePersistsRecipientsAndDispatchesAfterPersistence
()
{
NotificationEventDTO
event
=
new
NotificationEventDTO
();
event
.
setEventId
(
"event-1"
);
event
.
setMessageType
(
"LEAVE"
);
event
.
setTitle
(
"请假审批完成"
);
event
.
setContent
(
"审批结果:通过"
);
event
.
setCreatedAt
(
new
Date
());
NotificationMessageEntity
persisted
=
new
NotificationMessageEntity
();
persisted
.
setId
(
"message-1"
);
persisted
.
setEventId
(
"event-1"
);
persisted
.
setMessageType
(
"LEAVE"
);
persisted
.
setTitle
(
event
.
getTitle
());
persisted
.
setContent
(
event
.
getContent
());
persisted
.
setPriority
(
"NORMAL"
);
persisted
.
setCreatedAt
(
event
.
getCreatedAt
());
when
(
messageMapper
.
selectByEventId
(
"event-1"
)).
thenReturn
(
persisted
);
when
(
audienceService
.
resolve
(
event
)).
thenReturn
(
Arrays
.
asList
(
"user-1"
,
"user-2"
));
service
.
consume
(
event
);
verify
(
messageMapper
).
insertIgnore
(
any
(
NotificationMessageEntity
.
class
));
verify
(
recipientMapper
).
insertBatchIgnore
(
anyList
());
verify
(
unreadCache
,
times
(
2
)).
evict
(
any
());
ArgumentCaptor
<
NotificationPushDTO
>
pushCaptor
=
ArgumentCaptor
.
forClass
(
NotificationPushDTO
.
class
);
verify
(
pushDispatcher
).
dispatch
(
eq
(
Arrays
.
asList
(
"user-1"
,
"user-2"
)),
pushCaptor
.
capture
());
assertEquals
(
"NOTIFICATION"
,
pushCaptor
.
getValue
().
getEventType
());
assertEquals
(
"NEW_MESSAGE"
,
pushCaptor
.
getValue
().
getChangeType
());
assertTrue
(
pushCaptor
.
getValue
().
getRefreshUnreadCount
());
}
/** 验证标记已读后会通知同一用户的全部连接重新查询未读数。 */
@Test
void
markReadDispatchesUnreadCountChangedEvent
()
{
when
(
recipientMapper
.
markRead
(
eq
(
"message-1"
),
eq
(
"user-1"
),
any
(
Date
.
class
))).
thenReturn
(
1
);
boolean
changed
=
service
.
markRead
(
"message-1"
,
"user-1"
);
assertTrue
(
changed
);
verify
(
unreadCache
).
evict
(
"user-1"
);
ArgumentCaptor
<
NotificationPushDTO
>
pushCaptor
=
ArgumentCaptor
.
forClass
(
NotificationPushDTO
.
class
);
verify
(
pushDispatcher
).
dispatch
(
eq
(
Collections
.
singletonList
(
"user-1"
)),
pushCaptor
.
capture
());
assertEquals
(
"UNREAD_COUNT_CHANGED"
,
pushCaptor
.
getValue
().
getEventType
());
assertEquals
(
"MARK_READ"
,
pushCaptor
.
getValue
().
getChangeType
());
assertTrue
(
pushCaptor
.
getValue
().
getRefreshUnreadCount
());
}
/** 已经是已读状态时数据库更新数为0,不重复广播无意义的数量变化事件。 */
@Test
void
markReadDoesNotDispatchWhenStateDidNotChange
()
{
when
(
recipientMapper
.
markRead
(
eq
(
"message-1"
),
eq
(
"user-1"
),
any
(
Date
.
class
))).
thenReturn
(
0
);
boolean
changed
=
service
.
markRead
(
"message-1"
,
"user-1"
);
assertEquals
(
false
,
changed
);
verify
(
pushDispatcher
,
times
(
0
)).
dispatch
(
anyList
(),
any
());
}
/** 验证缓存失效和数量事件严格延迟到事务提交之后,防止客户端查询到旧数量。 */
@Test
void
unreadCountSynchronizationRunsOnlyAfterTransactionCommit
()
{
when
(
recipientMapper
.
markRead
(
eq
(
"message-1"
),
eq
(
"user-1"
),
any
(
Date
.
class
))).
thenReturn
(
1
);
TransactionSynchronizationManager
.
setActualTransactionActive
(
true
);
TransactionSynchronizationManager
.
initSynchronization
();
try
{
service
.
markRead
(
"message-1"
,
"user-1"
);
verifyNoInteractions
(
unreadCache
,
pushDispatcher
);
for
(
TransactionSynchronization
synchronization
:
TransactionSynchronizationManager
.
getSynchronizations
())
{
synchronization
.
afterCommit
();
}
verify
(
unreadCache
).
evict
(
"user-1"
);
verify
(
pushDispatcher
).
dispatch
(
eq
(
Collections
.
singletonList
(
"user-1"
)),
any
(
NotificationPushDTO
.
class
));
}
finally
{
TransactionSynchronizationManager
.
clearSynchronization
();
TransactionSynchronizationManager
.
setActualTransactionActive
(
false
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment