如何在spring boot中测试分页结果?

zwghvu4y  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(346)

我是编写单元测试的新手。我尝试了一些东西,并通过了saveproduct测试,但我不知道如何测试分页方法,并且不知道以这种方式编写测试是否是一种好方法。请告诉我我做错了什么,如何为我的服务实现类编写适当的单元测试?我还需要测试存储库接口吗?
我的代码结构如下

@Repository
public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {
  Optional<Page<Product>> findProductByMerchantId(Long id, Pageable pageable);
}
@Service
@RequiredArgsConstructor
public class ProductServiceImpl implements ProductService {

  private final ProductRepository repository;

  public List<Product> getAllProducts(
      Long merchantId, Integer pageNo, Integer pageSize, String sortBy) {
    Pageable paging = PageRequest.of(pageNo, pageSize, Sort.by(sortBy));

    Page<Product> pagedResult =
        repository
            .findProductByMerchantId(merchantId, paging)
            .orElseThrow(
                () ->
                    new IllegalStateException(
                        String.format("There is no product belong to %s ", merchantId.toString())));

    return pagedResult.getContent();
  }

  public Product saveProduct(Product product) {

    repository.save(product);

    return product;
  }
}
@ExtendWith(MockitoExtension.class)
class ProductServiceImplTest {

    @Mock
    ProductRepository productRepository;

    @InjectMocks
    ProductServiceImpl productServiceImpl;

    @Test
    void testGetAllProducts() {

        Product product=new Product();
        List<Product> list=new ArrayList<Product>();
        list.add(product);

        Pageable paging = PageRequest.of(0, 2, Sort.by("inventory"));

        when(productServiceImpl.getAllProducts(2L,0,2,"inventory")).thenReturn(list);

      // when(productRepository.findProductByMerchantId(2L,paging)).thenReturn(list);

        productServiceImpl.getAllProducts(2L,0,2,"inventory");

        verify(productRepository).findProductByMerchantId(2L,paging);
    }

    @Test
    void testSaveProduct() {
        Product product=new Product();

        when(productServiceImpl.saveProduct(product)).thenReturn(product);

        Product product1=productServiceImpl.saveProduct(product);

        assertNotNull(product1);

        verify(productRepository,times(1)).save(product);
    }
}
dl5txlt9

dl5txlt91#

您的方法是正确的,您必须模拟存储库接口并模拟调用。
如果您的存储库方法返回列表,请按如下所示使用

Mockito.when(appRepoitory.findProductByMerchantId(Mockito.anyString(), Mockito.any())).thenReturn(new ArrayList<>());

如果您的存储库方法返回页面,请按照以下步骤操作。

Page<Product> pro= Mockito.mock(Page.class);
    Mockito.when(appRepoitory.findProductByMerchantId(Mockito.anyString(), Mockito.any())).thenReturn(pro);

并验证类中是否有这些注解。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;

    @SpringBootTest
    @RunWith(MockitoJUnitRunner.class)
    public class ServiceTest {

    }

相关问题