zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
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
package com.tcm.training.admin.app.live.service;
 
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.tcm.training.admin.app.live.vo.AppLiveExpertVO;
import com.tcm.training.admin.live.entity.Expert;
import com.tcm.training.admin.live.mapper.ExpertMapper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
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.when;
 
/**
 * APP 养生直播专家服务测试。
 */
@ExtendWith(MockitoExtension.class)
class AppLiveExpertServiceTest {
 
    @BeforeAll
    static void initLambdaCache() {
        MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
        TableInfoHelper.initTableInfo(assistant, Expert.class);
    }
 
    @Mock
    private ExpertMapper expertMapper;
 
    private AppLiveExpertService appLiveExpertService;
 
    /**
     * 每个用例前重建被测服务。
     */
    @BeforeEach
    void setUp() {
        appLiveExpertService = new AppLiveExpertService(expertMapper);
    }
 
    /**
     * 已启用专家应返回 APP 端详情。
     */
    @Test
    void detailShouldReturnEnabledExpert() {
        Expert expert = new Expert();
        expert.setId(1L);
        expert.setName("张雨泽");
        expert.setAvatar("https://cdn.example/avatar.png");
        expert.setSpecialty("养生调理");
        expert.setIntro("专家介绍");
        expert.setStatus(1);
        when(expertMapper.selectOne(any())).thenReturn(expert);
 
        AppLiveExpertVO expertVO = appLiveExpertService.detail(1L);
 
        assertEquals(Long.valueOf(1L), expertVO.getId());
        assertEquals("张雨泽", expertVO.getName());
        assertEquals("养生调理", expertVO.getSpecialty());
    }
 
    /**
     * 禁用或不存在的专家应返回统一错误。
     */
    @Test
    void detailShouldFailWhenExpertMissing() {
        when(expertMapper.selectOne(any())).thenReturn(null);
 
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
                () -> appLiveExpertService.detail(99L));
 
        assertEquals("授课专家不存在", exception.getMessage());
    }
}