Java 我的测试是否正确?

Java 我的测试是否正确?,java,spring,unit-testing,junit,mockito,Java,Spring,Unit Testing,Junit,Mockito,我在服务类上有以下方法: @Service public class Service { (...) public Page<ChannelAccount> getByCustomerAndChannelType(Pageable pageable, Customer customer, ChannelType channelType) { return channelAccountRepository.findByCustomerAndChannel

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

@Service
public class Service {
    (...)
    public Page<ChannelAccount> getByCustomerAndChannelType(Pageable pageable, Customer customer, ChannelType channelType) {
        return channelAccountRepository.findByCustomerAndChannelType(pageable, customer, channelType);
    }
}
@服务
公务舱服务{
(...)
公共页getByCustomerAndChannelType(可分页、客户客户、频道类型ChannelType){
返回channelAccountRepository.findByCustomerAndChannelType(可分页、客户、渠道类型);
}
}
这将返回预期的结果。现在我正在尝试为它构建单元测试。到目前为止,我得到了这个:

@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);
    }
@RunWith(MockitoJUnitRunner.class)
公共类ChannelAccountServiceTest{
@注射模拟
私人渠道会计服务渠道会计服务;
@嘲弄
私有ChannelAccountRepository ChannelAccountRepository;
(...)
@试验
public void testGetByCustomerAndChannelTypePageable()测试{
Page pageResult=new PageImpl(channelAccountService.getAllChannelAccounts());
Mockito.when(channelAccountRepository.findByCustomerAndChannelType(pageable,customer,ChannelType.FACEBOOK)),然后返回(pageResult);
Page channelAccountPage=channelAccountRepository.findByCustomerAndChannelType(可分页,客户,ChannelType.FACEBOOK);
assertEquals(pageResult,channelAccountPage);
}

不知怎的,这感觉不对劲。我在这里遗漏了什么?

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

Page<ChannelAccount> pageResult = new PageImpl<>(channelAccountService.getAllChannelAccounts());
Page 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);
Pageable-pageableStub=Mockito.mock(Pageable.class);
pageStub=Mockito.mock(Page.class);
Mockito.when(channelAccountRepository)
.findByCustomerAndChannelType(pageableStub,customer,ChannelType.FACEBOOK))
。然后返回(页面存根);
Page channelAccountPage=channelAccountService
.findByCustomerAndChannelType(pageableStub,customer,ChannelType.FACEBOOK);
assertTrue(pageResult==channelAccountPage);

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

你能发布你是如何定义channelAccountService和channelAccountRepository的吗?也就是100%…这应该是一个单元测试而不是一个集成测试,对吗?不,测试除了Mockito之外不测试任何东西。根据它的名称,它应该测试服务,但它从不调用服务。它只调用模拟的reposito,并测试存储库是否返回您告诉它返回的内容。是的,单元测试。问题已编辑,显示如何将channelAccountService和channelAccountRepository插入测试类。