【问题标题】:Junit test (using when and Mockito verifyNoMoreInteractions)Junit 测试(使用 when 和 Mockito verifyNoMoreInteractions)
【发布时间】:2021-05-28 16:18:46
【问题描述】:

我正在对控制器方法进行 Junit 测试。其中一个方法有两个依赖项(从我的服务中调用两个不同的方法)因此我在@test 函数中使用了两次“when”。

@控制器

@Autowired
private CustomerAccountService customerAccountService;
@GetMapping("/customer/accountStatement")
public String accountStatement(Model model, HttpSession session) {
    
    LoginVO loginVO2 = (LoginVO) session.getAttribute("userSessionVO");
    
    if(loginVO2 != null) {
        Optional <CustomerAccountInfoVO> check = customerAccountService.customerInfo(loginVO2.getUsername());
        List <AccountStatementVO> newData = customerAccountService.accountStatement(check.get().getAccountNumber());
        model.addAttribute("accState", newData);
        return "customer/accountStatement";
    }else {
        return "customer/login";
    }

@Junit(像这样测试通过)

@Mock
private CustomerAccountService customerAccountService;

@InjectMocks
private CustomerUIController customerUIController;

private MockMvc mockMvc;

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.standaloneSetup(customerUIController).build();
    
}
@Test
public void testAccountStatement() throws Exception {
    
    HashMap<String, Object> session = new HashMap<String, Object>();
    LoginVO loginVO2 = new LoginVO();
    loginVO2.setUsername("Joe");
    loginVO2.setEmail("Joe@hotmail.com");
    session.put("userSessionVO", loginVO2);
    
    CustomerAccountInfoVO customerAccountInfoVO = new CustomerAccountInfoVO();
    customerAccountInfoVO.setAccountNumber("221");
    customerAccountInfoVO.setAccountType("Saving");
    customerAccountInfoVO.setAvBalance(123);
    customerAccountInfoVO.setCustomerId("Joe@hotmail.com");
    customerAccountInfoVO.setCurrency("Dollar");

    AccountStatementVO accountStatementVO = new AccountStatementVO();
    accountStatementVO.setAmount(3234);
    accountStatementVO.setCreditorAccountNumber("22331");
    accountStatementVO.setRemarks("hello");
    accountStatementVO.setDebitorAccountNumber("2211");
    List<AccountStatementVO> listdata = new ArrayList<AccountStatementVO>();
    listdata.add(accountStatementVO);
    
    when(customerAccountService.customerInfo(any(String.class))).thenReturn(Optional.of(customerAccountInfoVO));
    when(customerAccountService.accountStatement(any(String.class))).thenReturn(listdata);
    
    mockMvc.perform(get("/customer/accountStatement").sessionAttrs(session))
                            .andDo(print())
                            .andExpect(status().isOk())
                            .andExpect(view().name("customer/accountStatement"))
                            .andExpect(model().attribute("accState", hasItem(
                                    allOf(
                                            hasProperty("remarks", is("hello")),
                                            hasProperty("debitorAccountNumber", is("2211"))
                                    )
                                    )));
                            
    verify(customerAccountService, times(1)).customerInfo(any(String.class));
    verify(customerAccountService, times(1)).accountStatement(any(String.class));
    verifyNoMoreInteractions(customerAccountService);

错误 - 当我像这样运行我的方法时,出现错误(测试失败)
它说 - org.mockito.exceptions.verification.NoInteractionsWanted:这里不需要交互:

verify(customerAccountService, times(1)).customerInfo(any(String.class));
verifyNoMoreInteractions(customerAccountService);

通过测试-

verify(customerAccountService, times(1)).customerInfo(any(String.class));
verify(customerAccountService, times(1)).accountStatement(any(String.class));
verifyNoMoreInteractions(customerAccountService);

问题 -
我们是否必须像 when 一样多次调用 verify
当我们的控制器中有多个依赖项时,还有哪些其他方法可以执行 Junit 测试?

【问题讨论】:

    标签: java testing junit


    【解决方案1】:

    不,验证和何时不需要匹配。 如果您对验证每次交互不感兴趣,请不要致电verifyNoMoreInteractions。如果你调用它,你会告诉 mockito,你没有明确验证的一切都是意外行为,因此应该无法通过测试。

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2021-11-06
      • 2020-02-27
      • 1970-01-01
      相关资源
      最近更新 更多