【发布时间】: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 测试?
【问题讨论】: