简体   繁体   English

当 controller 中的 mocking 时,总是在数组列表中返回 NullPointerException

[英]Always return NullPointerException in array list when mocking in controller

I try to make simple unit test using JUnit and Mockito in the controller.我尝试在 controller 中使用 JUnit 和 Mockito 进行简单的单元测试。 I mocked the service because it will be called by the controller.我嘲笑了这个服务,因为它会被 controller 调用。 Here is the code这是代码

@RunWith(MockitoJUnitRunner.class)
class MsCustomerControllerTest {
    
    @Mock
    MsCustomerService customerServiceMock;

    
    @InjectMocks
    MsCustomerController customerController;
    
    @Test
    void test() {
        fail("Not yet implemented");
    }
    

    
    @Test
    public void findAllCustomerTest() {
        
        List<MsCustomer> listCustomer = new ArrayList<MsCustomer>();
        listCustomer.add(new MsCustomer(1, "Rosa", "Titian Indah", LocalDateTime.now()));
        listCustomer.add(new MsCustomer(2, "Rosa2", "Titian Indah2", LocalDateTime.now()));
        when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
        
        ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
        
        
        assertEquals(response, customerController.findAllCustomer());
    }

}

note: the customerController also return response entity, so assert with response entity too.注意:customerController 也返回响应实体,因此也使用响应实体进行断言。

Here is the result这是结果

在此处输入图像描述

I have tried other method and it also give me the NullPointerException.我尝试了其他方法,它也给了我 NullPointerException。

Try adding an init method and annotate it with @BeforeEach .尝试添加一个 init 方法并使用@BeforeEach对其进行注释。 Then inside the method add MockitoAnnotations.initMocks(this);然后在方法里面添加MockitoAnnotations.initMocks(this); which initializes fields annotated with Mockito annotations.它初始化使用 Mockito 注释注释的字段。

@RunWith(MockitoJUnitRunner.class)
class MsCustomerControllerTest {
    
    @Mock
    MsCustomerService customerServiceMock;

    
    @InjectMocks
    MsCustomerController customerController;

    @BeforeEach
    void initMock() {
        MockitoAnnotations.initMocks(this);
    }
    
    @Test
    void test() {
        fail("Not yet implemented");
    }
    

    
    @Test
    public void findAllCustomerTest() {
        
        List<MsCustomer> listCustomer = new ArrayList<MsCustomer>();
        listCustomer.add(new MsCustomer(1, "Rosa", "Titian Indah", LocalDateTime.now()));
        listCustomer.add(new MsCustomer(2, "Rosa2", "Titian Indah2", LocalDateTime.now()));
        when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
        
        ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
        
        
        assertEquals(response, customerController.findAllCustomer());
    }

}

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

相关问题 模拟控制器类时出现NullPointerException - NullPointerException when mocking controller class 在 SpringBoot API 中测试时 controller 上出现 NullPointerException - Mocking - NullPointerException on controller when testing in SpringBoot API - Mocking 当 mocking 为 QueryBuilder 时出现 NullPointerException - NullPointerException when mocking a QueryBuilder 模拟存储库 JUnit 时出现 NullPointerException - NullPointerException when mocking repository JUnit 通过套接字接收数组列表时发生NullPointerException - NullPointerException when receiving an array list over a socket 在数组列表上调用get函数时发生NullPointerException - NullPointerException when calling get function on array list 尝试将列表转换回数组时出现NullPointerException - NullPointerException when trying to convert list back into an array 在Spring中模拟身份验证:提供程序列表抛出NullPointerException - Mocking authentication in Spring: List of providers throws NullPointerException 当使用原始参数模拟方法时,Mockito中的NullPointerException - NullPointerException in Mockito when mocking method with primitive argument Mockito-模拟服务时抛出nullpointerException - Mockito - throws nullpointerException when mocking a service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM