简体   繁体   English

Null Page object in Spring Boot REST API unit test execution

[英]Null Page object in Spring Boot REST API unit test execution

I have the following REST API controller class.我有以下 REST API controller ZA2F2ED4F8EBC2CBB4C21A29DC40AB61D。

The endpoint retrieves a paged list of customers.端点检索客户的分页列表。

@RestController
public class CustomerController {
    
    @Autowired
    private CustomerRepository customerRepository;
    
    public void setCustomerRepositoryMock(CustomerRepository mockedCustomerRepository) {
        this.customerRepository = mockedCustomerRepository;
    }

    @GetMapping(value="/customers", produces = "application/json")
    public ResponseEntity<Page<Customer>> customersList(
        @RequestParam(value="pageNumber") int pageNumber,
        @RequestParam(value="pageSize") int pageSize){
        
        Pageable customersPageable = PageRequest.of(pageNumber, pageSize);
        
        Page<Customer> customersList = customerRepository.findAll(customersPageable);
        
        return new ResponseEntity<Page<Customer>>(customersList, HttpStatus.OK);
    }
}

Now I want to create a mocked unit test for that method.现在我想为该方法创建一个模拟单元测试。

This is what I have.这就是我所拥有的。

public class CustomerControllerTest {
    
    private CustomerRepository mockedCustomerRepository;
    private CustomerController customerController;
    private Customer customer;
    private Page<Customer> customersList;
    Pageable customersPageable;
    
    @BeforeEach
    void setup() {
    
        customer = new Customer();
        customer.setName("Pete");
        customer.setAge(35);
        customer.setEmail("pete@test.com");
        
        List<Customer> customersListTest = new ArrayList<Customer>();
        customersListTest.add(customer);    
        
        customersList = new PageImpl<>(customersListTest);
                
        mockedCustomerRepository = mock(CustomerRepository.class);
                
        customerController = new CustomerController();
        customerController.setCustomerRepositoryMock(mockedCustomerRepository);
        
    }
    
    @Test
    void testListCustomers() {

        when(mockedCustomerRepository.findAll(customersPageable)).thenReturn(customersList);    

        ResponseEntity<Page<Customer>> respPageCustomers = customerController.customersList(0, 3);
    
        assertTrue(respPageCustomers.getBody() != null);
        
    }
}

The problem is that when the following line is executed (in the API method), CustomerList is null.问题是当执行以下行时(在API方法中),CustomerList是null。

Page<Customer> customersList = customerRepository.findAll(customersPageable);

But it should have content, because the content was added in the setup method of the test class and then in the following line of the test method.但它应该有内容,因为该内容是在测试class的设置方法中添加的,然后在测试方法的以下行中。

when(mockedCustomerRepository.findAll(customersPageable)).thenReturn(customersList);

Replace代替

when(mockedCustomerRepository.findAll(customersPageable)).thenReturn(customersList);

with

when(mockedCustomerRepository.findAll(any(Pageable.class))).thenReturn(customersList);

What you currently have - is that mocked repository will return result only when it receives exact customersPageable (which is null).您目前拥有的是 - 模拟存储库仅在收到确切的 customersPageable (为空)时才会返回结果。
Using any() will return expected result if any object of mentioned class will be passed as parameter如果提到的 class 的任何 object 将作为参数传递,则使用 any() 将返回预期结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM