测试用例失败:参数不同!通缉:
Posted
技术标签:
【中文标题】测试用例失败:参数不同!通缉:【英文标题】:Test case failure : Argument(s) are different! Wanted: 【发布时间】:2018-10-18 02:49:01 【问题描述】:我正在学习 JUnit
和 mockito
。我正在尝试为 Account 控制器类中的搜索过滤器编写测试用例。但我得到 Argument(s) are different! Wanted:
failure 。谁能告诉我我在测试用例中做错了什么?
@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)
【问题讨论】:
【参考方案1】:如果有人仍然有这个问题,我从here得到了一个可行的解决方案
基本上,在大多数情况下,您只需要覆盖 Object.equals(Object) ,如果这不起作用,可能是因为您正在验证的对象无法更改或其 equals 函数无法被覆盖(无论出于何种原因超出了本文的上下文),然后使用org.mockito.Matchers.refEq(T value, String... excludeFields)
,如下所示:
verify(programServiceMock, times(1)).save(id, refEq(testpPList));
【讨论】:
以上是关于测试用例失败:参数不同!通缉:的主要内容,如果未能解决你的问题,请参考以下文章
EasyMock失败的测试用例“无效使用参数匹配器! 2匹配预期,1记录“错误
如何解决“论点不同! Junit 和 Mockito 中的通缉犯错误