【问题标题】:Testing Spring Controller with Mockito使用 Mockito 测试 Spring 控制器
【发布时间】:2021-12-15 13:32:44
【问题描述】:

我正在尝试学习使用 Spring 5、Mockito 和 JUnit 5 进行测试。 我有一个小的普通控制器类,它的测试如下:

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;
    
    @Autowired
    CustomerForm customerForm;

    @GetMapping("/index")
    public ModelAndView index(){
        String customerName = customerService.getCustomerById(14).getFirstname();  <-- Giving me error here
        customerForm.setCustomerName(customerName);

        ModelAndView modelAndView = new ModelAndView("customer/pages/customer/Home");
        modelAndView.addObject("customerForm", customerForm);
        return modelAndView;
    }
}
@ExtendWith(MockitoExtension.class)
class CustomerControllerTest {
    
    @InjectMocks
    CustomerController customerController;
    
    @Mock
    CustomerServiceImpl customerService;
    
    @Mock
    CustomerForm customerForm;
    
    Customer customer;
    String customerName;
    
    @SuppressWarnings("deprecation")
    @BeforeEach
    void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        customer = new Customer(14, "John");        
        customerName = "John";
    }

    @Test
    void testIndex() {
        int customerId = 14;
        when(customerService.getCustomerById(customerId).getFirstname()).thenReturn(customerName);  <-- Giving me error here, NullPointerException
        customerForm.setCustomerName(customerName);
        
        ModelAndView mav = customerController.index();
        assertEquals( customerForm, mav.getModel().get("customerForm"));
    }
}

错误:

java.lang.NullPointerException
    at com.primis.controller.CustomerControllerTest.testIndex(CustomerControllerTest.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)

当我运行这个测试时,我得到了NullPointerException,如图所示。 请有人指出我正确的方向,我做错了什么。

谢谢

【问题讨论】:

    标签: spring mockito


    【解决方案1】:

    你必须先模拟customerService.getCustomerById(customerId),否则它将返回null,在这种情况下,抛出NPE。

    【讨论】:

      【解决方案2】:

      你在嘲笑错误的东西:

      when(customerService.getCustomerById(customerId).getFirstname()).thenReturn(customerName);
      

      你真正需要模拟的只是调用customerService.getCustomerById()。这意味着您的测试代码应该是这样的:

      @ExtendWith(MockitoExtension.class)
      class CustomerControllerTest {
          
          @InjectMocks
          CustomerController customerController;
          
          @Mock
          CustomerServiceImpl customerService;
          
          @Mock
          CustomerForm customerForm;
          
          Customer customer;
          String customerName;
          
          @SuppressWarnings("deprecation")
          @BeforeEach
          void setUp() throws Exception {
              MockitoAnnotations.initMocks(this);
              customer = new Customer(14, "John");        
              customerName = "John";
          }
      
          @Test
          void testIndex() {
              int customerId = 14;
              when(customerService.getCustomerById(customerId)).thenReturn(customer);
              customerForm.setCustomerName(customerName);
              
              ModelAndView mav = customerController.index();
              assertEquals( customerForm, mav.getModel().get("customerForm"));
          }
      }
      

      附带说明,我相信使用@ExtendWith(MockitoExtension.class),您实际上并不需要明确的MockitoAnnotations.initMocks(this); 调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 2014-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多