简体   繁体   中英

Null Page object in Spring Boot REST API unit test execution

I have the following REST API controller class.

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.

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.

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).
Using any() will return expected result if any object of mentioned class will be passed as parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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