gpt4 book ai didi

java - 我的测试正确吗?

转载 作者:行者123 更新时间:2023-11-30 06:53:32 25 4
gpt4 key购买 nike

我在服务类上有以下方法:

@Service
public class Service {
(...)
public Page<ChannelAccount> getByCustomerAndChannelType(Pageable pageable, Customer customer, ChannelType channelType) {
return channelAccountRepository.findByCustomerAndChannelType(pageable, customer, channelType);
}
}

这将返回预期结果。现在我尝试为其构建单元测试。到目前为止我得到了这个:

@RunWith(MockitoJUnitRunner.class)
public class ChannelAccountServiceTest {
@InjectMocks
private ChannelAccountService channelAccountService;

@Mock
private ChannelAccountRepository channelAccountRepository;

(...)
@Test
public void testGetByCustomerAndChannelTypePageable() {
Page<ChannelAccount> pageResult = new PageImpl<>(channelAccountService.getAllChannelAccounts());
Mockito.when(channelAccountRepository.findByCustomerAndChannelType(pageable, customer, ChannelType.FACEBOOK)).thenReturn(pageResult);
Page<ChannelAccount> channelAccountPage = channelAccountRepository.findByCustomerAndChannelType(pageable, customer, ChannelType.FACEBOOK);
assertEquals(pageResult, channelAccountPage);
}

不知怎的,这感觉不太对劲。我在这里缺少什么?

最佳答案

不确定为什么要调用此方法,因为它与案例本身无关:

Page<ChannelAccount> pageResult = new PageImpl<>(channelAccountService.getAllChannelAccounts());

我会在测试中执行以下操作:

Pageable pageableStub = Mockito.mock(Pageable.class);
Page pageStub = Mockito.mock(Page.class);

Mockito.when(channelAccountRepository
.findByCustomerAndChannelType(pageableStub, customer, ChannelType.FACEBOOK))
.thenReturn(pageStub);

Page<ChannelAccount> channelAccountPage = channelAccountService
.findByCustomerAndChannelType(pageableStub, customer, ChannelType.FACEBOOK);

assertTrue(pageResult == channelAccountPage);

我会检查对象是否是相同的实例而不是 equals(甚至更严格)。

关于java - 我的测试正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com