liujie
2026-09-11 4f6f15b818704079ff2e4e4719b798f48d68a335
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
package com.tourguide.modules.finance.service;
 
import com.tourguide.modules.user.mapper.GuideUserMapper;
import com.tourguide.modules.user.mapper.GuideTeamMapper;
import com.tourguide.modules.user.entity.GuideTeam;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
 
import java.math.BigDecimal;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
class GuideWithdrawableBalanceServiceTest {
    private GuideUserMapper guideUserMapper;
    private GuideTeamMapper teamMapper;
    private GuideWithdrawableBalanceService service;
 
    @BeforeEach
    void setUp() {
        guideUserMapper = mock(GuideUserMapper.class);
        teamMapper = mock(GuideTeamMapper.class);
        service = new GuideWithdrawableBalanceService(guideUserMapper, teamMapper);
    }
 
    @Test
    void shouldReadStoredGuideBalance() {
        when(guideUserMapper.selectBalance(8L)).thenReturn(new BigDecimal("504.30"));
 
        assertThat(service.getAvailableAmount(8L)).isEqualByComparingTo("504.30");
    }
 
    @Test
    void shouldAtomicallyDebitStoredBalance() {
        when(guideUserMapper.decreaseBalance(8L, new BigDecimal("80.00"))).thenReturn(1);
 
        assertThat(service.debit(8L, new BigDecimal("80"))).isTrue();
        verify(guideUserMapper).decreaseBalance(8L, new BigDecimal("80.00"));
    }
 
    @Test
    void shouldReadAndAtomicallyDebitTeamBalance() {
        GuideTeam team = new GuideTeam();
        team.setBalance(new BigDecimal("504.30"));
        when(teamMapper.selectById(6L)).thenReturn(team);
        when(teamMapper.decreaseBalance(6L, new BigDecimal("80.00"))).thenReturn(1);
 
        assertThat(service.getAvailableTeamAmount(6L)).isEqualByComparingTo("504.30");
        assertThat(service.debitTeam(6L, new BigDecimal("80"))).isTrue();
        verify(teamMapper).decreaseBalance(6L, new BigDecimal("80.00"));
    }
}