我正在学习JUnit
和mockito
。我正在尝试从Account控制器类编写搜索过滤器的测试用例。但我得到Argument(s) are different! Wanted:
失败。谁可以告诉我在测试用例中我做错了什么?
@RestController
@RequestMapping("/api.spacestudy.com/SpaceStudy/Admin")
public class AccountController {
@Autowired
AccountService accService;
@GetMapping("/Account/findAccountData")
public ResponseEntity<List<Tuple>> btnSearchClick(String sClientAcctId, String sAcctDesc, String sInvestigatorName,
String sClientDeptId) throws Exception {
return ResponseEntity.ok(accService.btnSearchClick("1124100", sAcctDesc,sInvestigatorName,sClientDeptId));
}
}
测试用例
@RunWith(SpringRunner.class)
public class AccountControllerTest {
private MockMvc mockMvc;
@Mock
private AccountService accountService;
@InjectMocks
private AccountController accountController;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
}
@Test
public void btnSearchClickTest() throws Exception {
String sClientAcctId = "1124100";
String sAcctDesc = "SRIRAM";
String sInvestigatorName = "Ram, Sri";
String sClientDeptId = "120010";
Tuple mockedTuple = Mockito.mock(Tuple.class);
List<Tuple> accountObj = new ArrayList<>();
accountObj.add(mockedTuple);
Mockito.when(accountService.btnSearchClick(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
.thenReturn(accountObj);
mockMvc.perform(
get("/api.spacestudy.com/SpaceStudy/Admin/Account/findAccountData").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
Mockito.verify(accountService).btnSearchClick(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId);
}
}
堆栈跟踪
Argument(s) are different! Wanted:
accountService.btnSearchClick(
"1124100",
"SRIRAM",
"Ram, Sri",
"120010"
);
-> at com.spacestudy.controller.AccountControllerTest.btnSearchClickTest(AccountControllerTest.java:110)
Actual invocation has different arguments:
accountService.btnSearchClick(
null,
null,
null,
null
);
-> at com.spacestudy.controller.AccountController.btnSearchClick(AccountController.java:36)