【问题标题】:Test case failure : Argument(s) are different! Wanted:测试用例失败:参数不同!通缉:
【发布时间】:2018-10-18 02:49:01
【问题描述】:

我正在学习 JUnitmockito。我正在尝试为 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)

【问题讨论】:

    标签: unit-testing spring-data-jpa mockito junit4


    【解决方案1】:

    如果有人仍然有这个问题,我从here得到了一个可行的解决方案

    基本上,在大多数情况下,您只需要覆盖 Object.equals(Object) ,如果这不起作用,可能是因为您正在验证的对象无法更改或其 equals 函数无法被覆盖(无论出于何种原因超出了本文的上下文),然后使用org.mockito.Matchers.refEq(T value, String... excludeFields),如下所示:

    verify(programServiceMock, times(1)).save(id, refEq(testpPList));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2011-03-29
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      相关资源
      最近更新 更多